C++ code to Calculate SquareRoot of a number using Recursion
Example: Input: 25
Output: 5
Input: 11
Output:3
#include <iostream>
using namespace std;
void SquareRoot(int N,int x)
{
if(x*x==N)
{
cout<<x<<endl;
return;
}
else
{
if(x*x>N)
{
cout<<(x-1)<<endl;
return;
}
}
SquareRoot(N,x+1);
}
int main()
{
int N;
cout<<"Enter the number whose squareroot you want to know"<<endl;
cin>>N;
cout<<"The square root of the number is : ";
SquareRoot(N,1);
return 0;
}
Output: 5
Input: 11
Output:3
#include <iostream>
using namespace std;
void SquareRoot(int N,int x)
{
if(x*x==N)
{
cout<<x<<endl;
return;
}
else
{
if(x*x>N)
{
cout<<(x-1)<<endl;
return;
}
}
SquareRoot(N,x+1);
}
int main()
{
int N;
cout<<"Enter the number whose squareroot you want to know"<<endl;
cin>>N;
cout<<"The square root of the number is : ";
SquareRoot(N,1);
return 0;
}
Comments
Post a Comment