C program to find square root of a number
C program to find the square root of a number. Write a C program to enter the number and calculate the square root of a number using the sqrt function.
Write a C program to input the number and calculate the square root of the number using sqrt()
function.
To calculate square root of a number use sqrt()
function which is available under math.h
header file that's why include this header file also.
This problem can also be solved using loops if we don't want to use sqrt()
function but we will see this later in loops section.
Example -
Input:
Enter the number: 16
Output:
Square root of 16.00 is 4.00
C program to find square root of a number
#include <stdio.h>
#include <math.h>
void main()
{
double number, squareroot;
printf("Enter the number: ");
scanf("%lf", &number);
squareroot = sqrt(number);
printf("Square root of %0.2lf is %0.2lf", number, squareroot);
}
Output
Enter the number: 16
Square root of 16.00 is 4.00
Here, double
data type is used to store data and calculate square root of the number because the sqrt()
function returns double
type of data.
0.2lf
is used to print values upto 2 decimal places.