Find Square Root of a Number in C++

Google Advertisements

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

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. If you want to post your article on our website/FB Group/FB Page please contact on [email protected] or 8076671483

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: [email protected] or 8076671483

Path variable is set for providing path for all Java tools like java, javac, javap, javah, jar, appletviewer which are used in java programming. All these tools are available in bin folders so we set path upto bin folders.

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar.

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user.

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects.

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: [email protected] or 8076671483