C Program to Find the Square Root of a Number

In this article, we will write a C program to find the square root of a number. The program takes a number from the user as input, calculates its square root and prints it on the output screen.

Sample Example:

Input: 
Enter a number: 10
Output: 
The square root of 10.00 is: 3.1623

Input: 
Enter a number: 25
Output:
The square root of 25.00 is: 5.0000

The square root of a number is a value that, when multiplied by itself, gives the original number.

For example:

  • The square root of 9 is 3 because 3×3=9.
  • The square root of 16 is 4 because 4×4=16.
  • The square root of 25 is 5 because 5×5=25.

In C programming, to calculate the square root of a number we use the sqrt() function provided by the <math.h> header library.

The sqrt() function takes a single argument, which can be any positive number(decimal or integer) and returns the square root of the number as a result.

double sqrt(double x);

The following C program shows how you can find out the square root of a number:

// C Program to find the square root of a number

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

int main() {
	
	float num, square_root;
	
	printf("Enter a number: ");
	scanf("%f", &num);
	
	// Find the square root of the number
	square_root = sqrt(num);
	
    // Print the result
	printf("The square root of %.2f is: %.2f", num, square_root);

	return 0;

}

Output:

Enter a number: 16
The square root of 16.00 is: 4.00

Enter a number: 10
The square root of 10.00 is: 3.16

Program Explanation:

The program starts by taking a number from the user which is stored in the num variable.

On the next line, we used the sqrt() function to get the square root of the number entered by the user. The sqrt() function takes the number entered by the user as an argument and returns the result, which is stored in the square_root variable.

Finally, we print the square root of the number using the printf() function.


2. Using the pow() Function

There is one more way to find out the square root of a number, the pow() function.

The pow() function is actually an inbuilt function in C provided by the <math.h> header library. It is used to calculate the powers of a number, or more precisely, to raise a given base to a specified exponent.

Here is the syntax of the pow() function:

double pow(double base, double exponent);

Here, the base is the number whose power we want to calculate and exponent is the power to which the number is raised.

The main advantage of the pow() function is that it can accept the decimal value of the exponent. So, for example, if we want to calculate the square root of a number, we can pass the exponent as 0.5 which means half the power of the number.

square root of a number = pow(num, 0.5);

Let’s use this concept and calculate the square root of the number using the pow() function:

// C Program to find the square root of a number
// using the pow() function

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

int main() {
	
	float num, square_root;
	
	printf("Enter a number: ");
	scanf("%f", &num);
	
	// Find the square root of the number
	square_root = pow(num, 0.5);
	
    // Print the result
	printf("The square root of %.2f is: %.2f", num, square_root);

	return 0;

}

Output:

Enter a number: 49
The square root of 49.00 is: 7.00

These are the two most commonly used ways to find the square root of a number in C programming language. Both methods give the same output. You can use any of them.

I hope you will find this article helpful. Thanks for reading.

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

Leave a Comment