Square Root In C

  This tutorial will help to solve the square root problem using C Programming Language with or without predefined function.

Meaning Of Square Root

This section will help you to find the square root in C programming Language. For example, If there is a number ‘n’ , then we calculate the square of that number as n*n, it will generate a square of number as a results.

Now, what is the meaning of square root? It means, square root is just the reverse of the square number. For example, If there is a number called 36 then square root of this number is 6, because 6*6 will generate the square in mathematics. Same process is used in programming language to generate the square root.

Square root in C

Different method to calculate the Square root in C Programming

  1. Using SQRT() Method
  2. Using Pow() Method
  3. Using User defined Method

Table of Contents

Example 1 : C Program to calculate the Square Root using SQRT() method or function:

Let’s take an example to calculate the square root using SQRT() method in C Programming, This method uses the Math.h header file that contain the definition of sqrt() method.

   
 

/*;==========================================
; Title: Write a program  to calculate the Square Root.
; Owned: codenaive [email protected]  <[email protected]> <[email protected]>
; Contribution:  Santosh Kumar
; Programming Language: C
; Date:   30 Dec 2021
;==========================================*/

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

int main(){
	// declaration of the variables
	int value,result;
	double result2;
	
	// assign the value to calculate the square root
	value = 36;
	
	// calculate the square root
	result=sqrt(value);
	
	// display the results of integer value
	printf("The square root of %d is  %d. \n",value,result);
	
	// assign the value to calculate square root in double
	value = 37;
	
	// calculate the square root in double
	result2=sqrt(value);
	
	// display the results of integer value
	printf("The square root of %d is  %.4lf. \n",value,result2);
	
	
	
}


Output

The square root of 36 is  6.
The square root of 37 is  6.0828.

Example 2 : C Program to calculate the Square Root, take input from the user:

The following example will help you to get the input from the user and calculate the square root.

   
 

/*;==========================================
; Title: Write a program  to calculate the Square Root, take input from the user.
; Owned: codenaive [email protected]  <[email protected]> <[email protected]>
; Contribution:  Santosh Kumar
; Programming Language: C
; Date:   30 Dec 2021
;==========================================*/

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


int main(){
	// declaration of the variables
	int value,result;
	double value2, result2;
	
	printf("Enter Value to calculate the square root: ");
	// take the input from user and assign the value
	scanf("%d",&value);
	
	// calculate the square root
	result=sqrt(value);
	
	// display the results of integer value
	printf("The square root of %d is  %d. \n",value,result);
	
	
	printf("Enter Value(float) to calculate the square root: ");
	// take the float value from user and assign the value into value2 variable
	scanf("%lf",&value2);
	
	// calculate the square root in double
	result2=sqrt(value2);
	
	// display the results of integer value
	printf("The square root of %.4lf is  %.4lf. \n",value2,result2);
	
	
	
}


Output

Enter Value to calculate the square root: 25
The square root of 25 is  5.
Enter Value(float) to calculate the square root: 9.8
The square root of 9.8000 is  3.1305.

Example 3 : C Program to calculate the Square Root using Pow() method or function:

The following example shows the uses of pow() method and calculate the square root of number. Pow() method takes the two arguments value and the power respectively, we need to pass the 1/2 =0.5 as the default power to calculate the square root.

   
 

/*;==========================================
; Title: Write a program  to calculate the Square Root using Pow() function.
; Owned: codenaive [email protected]  <[email protected]> <[email protected]>
; Contribution:  Santosh Kumar
; Programming Language: C
; Date:   30 Dec 2021
;==========================================*/

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

int main(){
	// declaration of the variables
	int value;
	double result;
	
	// assign the value to calculate the square root
	value = 36;
	
	// calculate the square root, as we know the square root is th 1/2 of actual value, so the default value should be 0.5 
	result=pow(value,0.5);
	
	// display the results of integer value
	printf("The square root of %d is  %.2lf. \n",value,result);
	

	
	
}


Output

The square root of 36 is  6.00.

Example 4 : C Program to calculate the Square Root using user defined function:

The following example is calculating the square root using user defined function. It calculate the square root without using any predefined function.

   
 

/*;==========================================
; Title: Write a program  to calculate the Square Root using Pow() function.
; Owned: codenaive [email protected]  <[email protected]> <[email protected]>
; Contribution:  Santosh Kumar
; Programming Language: C
; Date:   30 Dec 2021
;==========================================*/

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

double mySqrt(double value){
	// declaration of the variables
	double temp=0,result;
	// divide the value to compare 
	result= value/2;
	// calculate at each iteration 
	while (result != temp)
    {  
        temp = result;       
        result = ( value / temp + temp) / 2;  
     }  
	 // return the result
	 return result; 
	
}
int main(){
	// declaration of the variables
	double value, result;
	
	
	printf("(User-Defined Function) \n Enter Value to calculate the square root: ");
	// take the float value from user and assign the value into value2 variable
	scanf("%lf",&value);
	
	// calculate the square root in double
	result=mySqrt(value);
	
	// display the results of integer value
	printf("The square root of %.4lf is  %.4lf. \n",value,result);
	
	
	
}


Output

(User-Defined Function)
 Enter Value to calculate the square root: 36
The square root of 36.0000 is  6.0000.

Leave a Reply

Your email address will not be published. Required fields are marked *