Find Square Root of a Number in C++


Prev Tutorial Next Tutorial

C++ program to find Square Root of a Number

For calculate Square Root of a number we multiply number by 0.5 because square root of any number means power of 1/2 and 1/2=0.5. And one another method for this program is use sqrt() function it is pre-defined in math.h header file.

C++ program to find Square Root

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

void main() 
{ 
int num,ans; 
clrscr(); 
cout<<"Enter any Num: "; 
cin>>num; 
ans=pow(num,0.5);
cout<<"\n Squre of "<<num<<" is: "<<ans; 
getch(); 
} 

Output

Enter any Num: 9
Squre of 9 is: 3

Explanation of Code

pow() is a predefined function in math.h header file, it is used for calculate power of any number.

C++ program to find Square Root

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

void main() 
{ 
int num,ans; 
clrscr(); 
cout<<"Enter any Number: "; 
cin>>num; 
ans=sqrt(num);;
cout<<"\n Squre of "<<num<<" is: "<<ans; 
getch(); 
} 

Output

Enter any Number: 9
Squre of 9 is: 3

Explanation of Code

sqrt() is a predefined function in math.h header file, it is used for calculate square root of any number.

Find Square Root By passing values in Function

Example

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

int square(int); 
void main()
{
int num;
clrscr();
cout<<"Enter any Num: ";
cin>>num;
cout<<"\n Square of "<<num<<" is: "<<square(num));
getch(); 
}
int square(int num)
{
int ans;
ans=pow(num,0.5);
return(ans); 
getch();
}

Output

Enter any Num: 64
Square of 3 is: 4

Prev Tutorial Next Tutorial

Google Advertisements
Download Projects

Google Adsense Advertisements

Buy 3 Year Old Domain, Tutorialday.com, SeosmoTools.com @$200

Buy This Ad Space @$50 per Month, Ad Size 300X250 Contact on: hitesh.xc@gmail.com WhatsApp no. 8882538826

Yahoo Advertisements