Site icon T4Tutorials.com

Progrm to find the squar root of a number in c, cpp,cplus plus

Program to find the square root of a number in c and CPP,c plus plus

In this tutorial, we will try to learn the followings;

  1. How to draw the flow chart of the square root of a number?
  2. How to find the square root of a number in C++?
  3. How to find the square root of a number in C?

Flow chart of the square root of a number

Figure: Program to find the square root of a number in c, CPPC++.

The square root of a number in C++

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
  float NO, answer_of_GivenNO;
  cout <<"Enter number to find its squareroot: ";
  cin >> NO;
  answer_of_GivenNO = pow(NO, 0.5);
  cout<<"Squreroor of "<<NO<<" is: " << answer_of_GivenNO << endl;
}

Output:

Figure: square root output.

Square root of only a Positive Number repeatedly using If else statement in C++

Let’s see the “program of the square root of a positive number in C++”.

#include<iostream>
#include<Math.h>
using namespace std;
int main()
{
 float no;
 cout<<"Enter a number to find its square root: ";
 cin>>no;
 
 if(no >=0 )
{
cout<<"the square root of no is "<<sqrt(no)<<endl;	
}
else
{
	cout<<"Sorry it is a negative number"<<endl;
 } 
 
 
 return 0;
	
	
}

Output Sample 1

Enter a number to find its square root: -4
Sorry it is a negative number

Output Sample 2

Enter a number to find its square root: 4
the square root of no is 2

The square root of a number repeatedly using nested if statement  in C++

Let’s see the “program of the square root of a number using nested if statement in C++”.

#include<iostream>
#include<Math.h>
using namespace std;
int main()
{
 float no;
 cout<<"enter a number to find its squareroot : "<<endl;
 cin>>no;
 
 if(no==0){
 	cout<<"You Entered 0 So the Square Root Is 0 "<<endl;
 }
else if(no > 0 )
{
cout<<"squareroot of no is "<<sqrt(no)<<endl;	
}
else
{
	cout<<"Sorry it is a negative number"<<endl;
}  
 return 0;	
}

The square root of a number repeatedly using a do-while loop in C++

Let’s see the “program of the square root of a number repeatedly using do-while Loop in C++”.

#include<iostream>
#include<Math.h>
using namespace std;
int main()
{
int no;
int times;
cout<<"Enter No of times you want to Find Square Root "<<endl;
cin>>times;
int i=0;
do{
 float no;
 cout<<"enter a number to find its squareroot : ";
 cin>>no; 
 if(no >=0 )
{
cout<<"squareroot of no is "<<sqrt(no)<<endl;	
}
else
{
	cout<<"complex root"<<endl;
 } 
i++;
}while(i<times);
 return 0;	
}

Output:
Enter No of how many times you want to Find Square Root
2
Enter Value
3
Enter Value
4
The square root of 3 is 1.73205
The square root of 4 is 2

Square root of a number repeatedly using a For loop in C++

Let’s see the “program of the square root of a number repeatedly using For Loop in C++”.

#include<iostream>
#include<Math.h>
using namespace std;
int main()
{
int no;
int times;
cout<<"Enter No of times you want to Find Square Root "<<endl;
cin>>times;
for(int i=0;i<times;i++){	
 float no;
 cout<<"enter a number to find its squareroot : ";
 cin>>no; 
 if(no >=0 )
{
cout<<"squareroot of no is "<<sqrt(no)<<endl;	
}
else
{
	cout<<"complex root"<<endl;
 } 
}
 return 0;	
}

Output:
Enter No of how many times you want to Find Square Root
2
Enter Value
3
Enter Value
4
The square root of 3 is 1.73205
The square root of 4 is 2

The square root of a number repeatedly using Array in C++

Let’s see the “program of the square root of a number using arrays in C++”.

Array: float array[HowManyTimes];

#include<iostream>
#include<Math.h>
using namespace std;
int main()
{
int HowManyTimes;
cout<<"Enter No of How Many Times you want to Find Square Root "<<endl;
cin>>HowManyTimes;
float array[HowManyTimes];
for(int i=0;i<HowManyTimes;i++){
	cout<<"Enter Value "<<i+1<<endl;
	cin>>array[i];
}
for(int i=0;i<HowManyTimes;i++){
	 
 if(array[i] >=0 )
{
cout<<"The Square root of "<<array[i]<<"is "<<sqrt(array[i])<<endl;	
}
else
{
	cout<<"complex root"<<endl;
 }  
}
 return 0;	
}

Output:


Enter No of how many times you want to Find Square Root
2
Enter Value
3
Enter Value
4
The square root of 3 is 1.73205
The square root of 4 is 2

The square root of a number in C?

#include <stdio.h>
#include <math.h>
int main()
{
    double no, root_of_no;
    printf("Enter number to find its root: ");
    scanf("%lf", &no);
    root_of_no = sqrt(no);
    printf("Square root of %.2lf = %.2lf", no, root_of_no);
}

Output:

Figure: square root output.
Exit mobile version