Square Root In C

In C programming, the sqrt function is a built-in library function employed to compute the square root of a value. The sqrt function is specified in the math.h header file. Hence, it is essential to include the math.h header file when utilizing the sqrt function in C. Additionally, there are alternative methods to determine the square root of a given number without relying on the sqrt function.

Syntax of the sqrt function

Example

double sqrt( double arg);

In the above syntax, the sqrt function takes a single argument as double to return its square root in double data types.

arg: It represents a double data type parameter within the sqrt function.

The return value of the sqrt function is the square root of the specified number within a defined double data type.

Note: We can find the square root of the int, float, double or long double data type number by explicitly converting the given data type to another.

Algorithm to find the Square Root

  • Declare an integer variable, as num.
  • Use the sqrt function to pass the num variable as an argument to find the square root.
  • Print the result.
  • Exit or terminate the program.
  • Example 1: Program to get the square root of a number using the sqrt function

Let's explore an illustration to determine the square root of a specified number by utilizing the sqrt function in the C programming language.

Example

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

int main ()
{
	// declaration of the int, float and double variables
	int x, res;
	float y, res1;
	double z, res2;
	
	x = 289;
	// use the sqrt() function to return integer values 
	res = sqrt(x); 
	printf (" The square root of %d is: %d", x, res);

	// square root of float variable
	y = 12.25;
	// use the sqrt() function to return float values 
	res1 = sqrt(y); 
	printf (" \n The square root of %.2f is: %.2f", y, res1);
		
           // square root of double variable
	z = 144.00;
	// use the sqrt() function to return double values 
	res2 = sqrt(z); 
	printf (" \n The square root of %.2lf is: %.2lf", z, res2);
	
	return 0;	
}

Output:

Output

The square root of 289 is: 17
The square root of 12.25 is: 3.50
The square root of 144.00 is: 12.00

Example 2: Program to take a number from user and to get the square root

Let's explore a scenario where we obtain user input to calculate and display the square root of a given number using the sqrt function in the C programming language.

Example

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

int main ()
{
	// declare an integer variable
	int x;
	double res;
	printf (" Enter any number to get the square root: ");
	scanf (" %d", &x);
	// use the sqrt() function to return integer values 
	res = sqrt(x); 
	printf (" \n The square root of %d is: %.2lf", x, res);

	return 0;	
}

Output:

Output

Enter any number to get the square root: 625
The square root of 625 is: 25.00

Example 3: Program to find the square root using user defined function

Let's develop a program that calculates the square root of a specified number by utilizing a custom function in the C programming language.

Example

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

// function declaration
double getSqr (int num);

int main ()
{
	// declare an integer variable
	int x;
	double res;
	printf (" Enter any number to get the square root: ");
	scanf (" %d", &x);

	res = getSqr(x); // call the function
	printf (" \n The square root of %d is: %.2lf", x, res);

	return 0;	
}

// function definition
double getSqr ( int num)
{
	double getRes;
	// use sqrt() function to print the square root
	getRes = sqrt (num);
	return getRes;
}

Output:

Output

Enter any number to get the square root: 87
The square root of 87 is: 9.33

pow function

The pow function is a built-in function within the math.h header file that computes the exponentiation of a specified number.

Syntax of the pow function

Example

int pow( arg, 0.5);

The pow function requires two parameters: the initial parameter specifies the base number to be raised to a power or to have its square root calculated, while 0.5 serves as a preset argument equivalent to ½ or 1 / 2 = 0.5.

Example 4: Program to get the square root of a number using the pow function

Let's explore an illustration demonstrating how to calculate the square root of a number by utilizing the pow function in the C programming language.

Example

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

int main ()
{
	// declare an integer variable
	int x;
	double res;
	printf (" Enter any number to get the square root: ");
	scanf (" %d", &x);
	// use the pow() function to return the square root  
	res = pow(x, 0.5); //it takes two argument: input variable and 0.5 is default value
	printf (" \n The square root of %d is: %.2lf", x, res);

	return 0;	
}

Output:

Output

Enter any number to get the square root: 1225
The square root of 1225 is: 35.00

In the provided code snippet, we acquire the integer input value 1225 for the variable x from the user. This value is subsequently passed as an argument to the pow function to calculate and return either the power or square root of the provided number.

Example 5: Program to get the square root of a number without using the sqrt function

Let's explore a scenario where we calculate the square root of a number in C without relying on the built-in sqrt function.

Example

/* Display the square root of a number without using the sqrt() function in C. */
#include <stdio.h>
#include <math.h>
#include <conio.h>
int main()
{
	// declaration of the variables
	int num;
	float sqrt, temp;
	
	printf (" Enter a number to get the square root: ");
	scanf (" %d", &num);
	
	// divide the given number by 2 and store into sqrt
	sqrt = num / 2;
	temp = 0;
	
	// use while loop to continuously checks the sqrt is not equal to the temp
	while (sqrt != temp) // Initially temp is 0 and sqrt = num
	{
		temp = sqrt; // assign sqrt to temp
		
		sqrt = ( num / temp + temp) / 2;
	 } 
	 
	 printf (" \n The square root of %d is %f", num, sqrt);
	 return 0;
}

Output:

Output

Enter a number to get the square root: 2
The square root of 2 is 1.414214

In the provided code snippet, a user's number input is utilized to calculate its square root. Initially, the input number is divided by 2 and saved in the sqrt variable. Subsequently, the temp variable is set to 0. A while loop is employed to iteratively compare the sqrt value with temp, updating temp with the current sqrt value in each iteration. The sqrt variable is recalculated using the formula (num/temp + temp) / 2. Finally, the output displays the square root of 2 as approximately 1.414214.

Input Required

This code uses input(). Please provide values below: