C program to find square root of a number

Previous Program Next Program

Write a C program to input any number and find square root of the given number. How to calculate square root of any number in C programming using inbuilt sqrt() function. How to use predefined sqrt() function to find square root in C program.

Example

Input

Enter any number: 9

Output

Square root of 9 = 3

Required knowledge

Fundamentals of C, Data types, Talking user input in C

Program to find square root of any number

/**
 * C program to find square root of a number
 */

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

int main()
{
    double num, root;

    // Reads the number to find square root
    printf("Enter any number to find square root: ");
    scanf("%lf", &num);

    //Calculates square root of num
    root = sqrt(num);

    printf("Square root of %.2lf = %.2lf", num, root);

    return 0;
} 
Output
Enter any number to find square root: 144
Square root of 144.00 = 12.00

Happy coding ;)

Recommended posts

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment