Cal11 calculator

Simple C Program to Calculate Square Root of A Number

Reviewed by Calculator Editorial Team

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:

  1. #include <stdio.h> - This line includes the standard input/output library, which is needed for the printf() and scanf() functions.
  2. #include <math.h> - This line includes the math library, which contains the sqrt() function.
  3. double number, square_root; - These lines declare two variables of type double (floating-point numbers) to store the input number and the calculated square root.
  4. printf("Enter a number: "); - This line prompts the user to enter a number.
  5. scanf("%lf", &number); - This line reads the user's input and stores it in the number variable.
  6. 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 the sqrt() function and prints the result.
  7. square_root = sqrt(number); - This line calculates the square root of the input number using the sqrt() function and stores the result in the square_root variable.
  8. printf("Square root of %.2lf is %.2lf\n", number, square_root); - This line prints the result, formatted to two decimal places.
  9. return 0; - This line indicates that the program has executed successfully.

How This Program Works

The program works by:

  1. Prompting the user to enter a number.
  2. Reading the user's input and storing it in the number variable.
  3. Checking if the input number is negative. If it is, the program prints an error message and exits.
  4. If the input number is non-negative, the program calculates the square root using the sqrt() function and stores the result in the square_root variable.
  5. 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 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.
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 -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.
Can I calculate the square root of a number without using the math.h library?
Yes, you can calculate the square root of a number without using the math.h library, 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-in sqrt() function.