Find Square Root of a Number in C++

Advertisements

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.

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
```

Advertisements

Magenet is best Adsense Alternative here we earn \$2 for single link, Here we get links ads. Magenet

Google Advertisements

Buy Websites 9999595223

Wifi CCTV Camera RS:1200 to 2499. Contact no. 9999595223

Advertisements