Problem Solving: Integer Roots
Learn to write a program that finds the square root of a number.
We'll cover the following
Integer square root
We want to make a program that takes inputs until the user enters -1
and tells whether the number is a perfect square. If yes, of which number (make a separate function to return this number). If not, it should print its integer square root.
Sample input
25 8 -1
Sample output
25 is a perfect square of 5
8 is not a perfect square. Its integer square-root is 2
Have you noticed that
25
is a perfect square of5
? For example,5x5
is equal to25
and if we take the square root of25
, it will return5
.
Let’s make another function called integerSquareRoot()
that returns the square root of an integer.
We can simply modify the following function which we discovered in the last lesson, bool isPerfectSquare(int num)
.
bool isPerfectSquare(int n){for(int s = 0; s*s <= n ; s++){if(s*s==n){return true;}}return false;}
We can simply modify the above functions to find the integer square root.
int integerSquareRoot(int n){for(int s=0;s*s <= n;s++) // the loop will end with the first s having property s*s>n.{// This loop will end when s is one greater than the integer square root of N.}return s-1; // as s*s>n: hence s-1 will be either perfect square root or integer part of the square root.}
Let us write the complete code below and experiment in the coding playground.
#include <iostream> using namespace std; //this function tells us that either the number is perfect square or not. bool isPerfectSquare(int); /*this function tells us that the number is perfect square or which of the closest number.*/ int integerSquareRoot(int); int main() { int num=0; cout << "Enter Stream (for finding its square-root, ending with -1) : "; cin>>num; for(int cnt=0;num!=-1;cnt++) { if(isPerfectSquare(num)) { cout<<" The number is a perfect square of : " <<integerSquareRoot(num)<<" .\n"; } else { cout<<" The number is not a perfect square. It’s integer square root of : " <<integerSquareRoot(num)<<" .\n"; } cin>>num; } return 0; } bool isPerfectSquare(int n) { for(int s = 0; s*s <= n ; s++) { if(s*s==n) { return true; } } return false; } int integerSquareRoot(int n) { int s; for(s=0; s*s <= n;s++) { // This loop will end when s will be one greater than integer square root of N. } return s-1; }
- In line 15, we have checked whether the
num
is a perfect square or not. - In lines 18 and 23, we are printing the square root of the number by calling the
integerSquareroot()
function.
Exercise 1: Finding the integer cube root
Modify the above program to report the integer cube root of a positive integer.
Sample program
Input Stream: 27 64 126 217 -1
27 is a perfect cube, having a cube root of 3
64 is a perfect cube, having a cube root of 4
126 is not a perfect cube, its approximate integer cube root is 5.
217 is not a perfect cube, its approximate integer cube root is 6.
#include <iostream> using namespace std; //this function tells us that either the number is perfect square or not. bool isPerfectCube(int); /*this function tells us that the number is perfect square or which of the closest number.*/ int integerCubeRoot(int n); int main() { int num=0; cout << "Enter Stream (for finding its square-root, ending with -1) : "; cin>>num; for(int cnt=0;num!=-1;cnt++) { if(isPerfectCube(num)) { cout<<" The number is a perfect cube of : " <<integerCubeRoot(num)<<" .\n"; } else { cout<<" The number is not a perfect cube. It’s integer approximate cube root of : " <<integerCubeRoot(num)<<" .\n"; } cin>>num; } return 0; } bool isPerfectCube(int n) { // Write code here } int integerCubeRoot(int n) { // Write code here }
Exercise 2: Finding the integer Kth root
Modify the above program to report the K’th cube root of a positive integer.
Here is a sample program
Input Stream: 81 16 126 317 -1
K: 4
81 is a perfect 4'th power, of 3
16 is a perfect 4'th power, of 2
126 is not a perfect 4'th power, its integer approximate is 3.
317 is not a perfect 4'th power, its integer approximate is 4.
Instruction: Add the following functions in the above playground.
int Power(int a, int k);
bool isPerfectKthPower(int n, int K);
int integerKthRoot(int n);