You are on page 1of 1

//==================================================================

//Exercise 9 : WAP to find the square root using inline function.


//==================================================================
#include <iostream.h>
#include <conio.h>
#include <math.h>

//------------------------------------------------------
// Function to compute sqrt using Math function sqrt()
//------------------------------------------------------
inline float Fsqrt(float n){
return sqrt(n);
}
//------------------------------------------------------
// Function to compute sqrt using logic
//------------------------------------------------------
float Msqrt(float n){
int s;
for(s = 1; s*s <= n; s++);
s--;

double x;
for(double d = 0.001; d < 1.0; d+= 0.001){
x = (double)s + d;
if((x*x > (double)n)){
x -= 0.001;
break;
}
}
return x;
}
//------------------------------------------------------
// Main Function to compute sqrt(n)
//------------------------------------------------------
void main(){
float n;
clrscr();
cout<<"\n=======================================================";
cout<<"\nProgram to find square root of a number";
cout<<"\n=======================================================";
cout<<"\nEnter a number to compute square root : ";cin>>n;
cout<<"\nComputed square root of "<<n<<" is : "<<Msqrt(n);
cout<<"\nReturned square root of "<<n<<" is : "<<Fsqrt(n);
cout<<"\n=======================================================";
getch();
}

You might also like