C Program To Find Square Root of a Number

In this post, we will learn how to find square root of a number using the C Programming language.

C Program To Find Square Root of a Number

A Square root of a number is a value that, when multiplied by itself, gives the number. For example: 5 x 5 = 25, so the square root of 25 is 5.

We will be calculating the square root of any number entered by the user.

So, without further ado, let’s begin this tutorial.

C Program To Find Square Root of a Number

// C Program To Find Square Root of a Number
#include <stdio.h>
#include <math.h>

int main(){
    double num, root;
    
    // Asking for Input
    printf("Enter an integer: ");
    scanf("%lf", &num);
    
    root = sqrt(num);
    printf("The Square Root of %.2lf is %.2lf.", num, root);
    return 0;
}

Output

Enter an integer: 2
The Square Root of 2.00 is 1.41.

How Does This Program Work ?

    double num, root;

In this program, we have declared two double data type variables named num and root.

    // Asking for Input
    printf("Enter an integer: ");
    scanf("%lf", &num);

Then, the user is asked to enter the value of a number. The value of this number will get stored in the num named variable.

root = sqrt(num);

We calculate the square root of the number using sqrt() function. sqrt() function is used to find the square root of the given number.

    printf("The Square Root of %.2lf is %.2lf.", num, root);

Finally, the square root of the entered number is printed to the screen using printf() function. Here, we have used %.2lf format specifier, because we want to display the result only till 2 decimal places.

C Program To Find Square Root of a Number Without Using sqrt

// C Program To Find The Square Root of a Number Without Using Sqrt
#include <stdio.h>
#include <math.h>

int main(){
    double num, root;
    
    // Asking for Input
    printf("Enter a number: ");
    scanf("%lf", &num);
    
    root = pow(num, 0.5);
    printf("The Square Root of %.2lf is %.2lf.", num, root);
    return 0;
}

Output

Enter a number: 49
The Square Root of 49.00 is 7.00.

Conclusion

I hope after going through this post, you learn how to find square root of a number in the C Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published.