C Program to Find Square Root of a Number using sqrt Function

The C Program to Find the Square Root of a Number using the sqrt function is a simple yet essential exercise that introduces beginners to mathematical operations and function usage in C programming.

In this C program we will learn to find square root using sqrt function. We will first take a number as input from user using scanf function then calculate square root of number by calling sqrt function of math.h header file.


Tips for Writing Square Root Program using sqrt in C

When writing the C Program to Find the Square Root of a Number using the sqrt function, follow these tips to ensure clarity and correctness in your code:
  • Include Necessary Headers : Begin your program by including the necessary headers, such as for input and output operations and for mathematical functions like sqrt.

  • Use scanf for Input : Use the scanf function to input the number for which you want to find the square root. Ensure that the input is valid and within the range of the data type you are using.

  • Declare Variables : Declare variables to store the input number and the result of the square root computation. Use meaningful variable names to enhance readability.

  • Use sqrt Function : Utilize the sqrt function from the header to calculate the square root of the input number. Remember that sqrt returns a double value.

  • Display the Result : Print the result using the printf function, ensuring that the output is clear and understandable. Consider formatting the output to display the result with an appropriate level of precision.

  • Error Handling : Implement error handling to deal with scenarios where the input number is negative or invalid. You can use conditional statements (if and else) to check for such cases.

  • Compile with Math Library : When compiling your program, remember to link the math library using the -lm flag in GCC to enable the use of the sqrt function.

  • Data Type for Result : The sqrt function returns a double value, so ensure that the variable used to store the result is of type double.

  • Test Your Program : Before finalizing your code, test it with different input values to ensure that it produces the correct square root for each case.

Required Knowledge


C program to find square root of a number

#include <stdio.h>
#include <math.h>
 
int main () {
    double x, result;
    printf("Enter a number\n");
    scanf("%lf", &x);
   
    result = sqrt(x);
    printf("Square root of %lf = %lf\n", x, result);
   
    return 0;
}
Output
Enter a number
49
Square root of 49.000000 = 7.000000
Enter a number
85.453
Square root of 85.453000 = 9.244079

Conclusion

In conclusion, the C Program to Find the Square Root of a Number using the sqrt function is a valuable exercise that introduces beginners to mathematical operations and function usage in C programming. Practicing this program helps beginners develop problem-solving skills and understand the practical application of mathematical concepts in programming.

Related Topics
C program to find all roots of quadratic equationp
C program to check whether a number is prime or not
C program to add digits of a number
C program to count number of digits in an integer
C program to find sum of digits of a number using recursion
C program to reverse a number using recursion
C program to calculate power of a number
C program to generate random numbers
C program to convert string to integer
List of all C programs