Simple C Program to Calculate Square Root of A Number
Calculating the square root of a number is a fundamental mathematical operation that can be implemented in the C programming language using the standard library's math functions. This guide explains how to write a simple C program to calculate the square root of a number using the math.h library and the sqrt() function.
How to Calculate Square Root in C
To calculate the square root of a number in C, you'll need to use the sqrt() function from the math.h library. This function takes a single argument (the number you want to find the square root of) and returns the square root as a floating-point number.
Formula: sqrt(x) where x is the number you want to find the square root of.
Before you can use the sqrt() function, you need to include the math.h header file in your program. This header file contains the declaration of the sqrt() function and other mathematical functions.
Note: When using the sqrt() function, make sure to compile your program with the -lm flag to link the math library. For example, if your program is named sqrt.c, you would compile it with the command gcc sqrt.c -o sqrt -lm.
Example C Program
Here's a simple C program that calculates the square root of a number entered by the user:
#include <stdio.h>
#include <math.h>
int main() {
double number, square_root;
printf("Enter a number: ");
scanf("%lf", &number);
if (number < 0) {
printf("Error: Cannot calculate square root of a negative number.\n");
} else {
square_root = sqrt(number);
printf("Square root of %.2lf is %.2lf\n", number, square_root);
}
return 0;
}
Let's break down this program:
#include <stdio.h>- This line includes the standard input/output library, which is needed for theprintf()andscanf()functions.#include <math.h>- This line includes the math library, which contains thesqrt()function.double number, square_root;- These lines declare two variables of typedouble(floating-point numbers) to store the input number and the calculated square root.printf("Enter a number: ");- This line prompts the user to enter a number.scanf("%lf", &number);- This line reads the user's input and stores it in thenumbervariable.if (number < 0) { ... } else { ... }- This conditional statement checks if the input number is negative. If it is, the program prints an error message. Otherwise, it calculates the square root using thesqrt()function and prints the result.square_root = sqrt(number);- This line calculates the square root of the input number using thesqrt()function and stores the result in thesquare_rootvariable.printf("Square root of %.2lf is %.2lf\n", number, square_root);- This line prints the result, formatted to two decimal places.return 0;- This line indicates that the program has executed successfully.
How This Program Works
The program works by:
- Prompting the user to enter a number.
- Reading the user's input and storing it in the
numbervariable. - Checking if the input number is negative. If it is, the program prints an error message and exits.
- If the input number is non-negative, the program calculates the square root using the
sqrt()function and stores the result in thesquare_rootvariable. - Printing the result, formatted to two decimal places.
This program demonstrates how to use the sqrt() function from the math.h library to calculate the square root of a number in C. The program also includes error handling to ensure that the user enters a valid input.
Frequently Asked Questions
- What is the square root of a number?
- 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.
- How do I calculate the square root of a number in C?
- To calculate the square root of a number in C, you can use the
sqrt()function from themath.hlibrary. This function takes a single argument (the number you want to find the square root of) and returns the square root as a floating-point number. - What happens if I try to calculate the square root of a negative number?
- If you try to calculate the square root of a negative number, the
sqrt()function will return a special value called NaN (Not a Number). This is because the square root of a negative number is not a real number. - How do I compile a C program that uses the
sqrt()function? - When you compile a C program that uses the
sqrt()function, you need to include the-lmflag to link the math library. For example, if your program is namedsqrt.c, you would compile it with the commandgcc sqrt.c -o sqrt -lm. - Can I calculate the square root of a number without using the
math.hlibrary? - Yes, you can calculate the square root of a number without using the
math.hlibrary, but it requires implementing a numerical method such as the Newton-Raphson method. This approach is more complex and less efficient than using the built-insqrt()function.