Exploring the sqrt() Function in C programming language

24 Feb 2023 Balmiki Mandal 0 C Programming

Understanding the sqrt() Function in C Programming

The sqrt() function is a built-in mathematical function in the C programming language that is used to calculate the square root of a given number. It is defined in math.h header file.

The syntax of the sqrt() function is as follows

double sqrt(double x);

where x is the argument whose square root is to be calculated. The return type of the sqrt() function is double, which is the square root of the argument x.

Here is an example code snippet that demonstrates how to use the sqrt() function in C:

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

int main() {
    double num, result;
    
    printf("Enter a number: ");
    scanf("%lf", &num);
    
    result = sqrt(num);
    
    printf("Square root of %.2lf = %.2lf", num, result);
    
    return 0;
}

In this example, the user is prompted to enter a number. The scanf() function is used to read the number from the user, and the sqrt() function is used to calculate the square root of the number. The result is then displayed using the printf() function.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.