Program to find square root of a number in C
In this post, you will learn how to write a program to find the square root of a number using the C programming language.
In mathematics, the square root of a number is the number that we need to multiply by itself to get the original number. If a is the square root of b, it means that a×a=b. For example, the square root of 9, √9 = 3. Such a type of question is generally asked in competitive exams and interviews.
Square root in c using sqrt() method
We can use the standard library function sqrt() to find the square root of a number. It accepts a single argument of type double and returns the square root of that number. The sqrt() is defined in the math.h library. We need to include the "math.h" header file in the program to use this method.
/**
* C program to get square root of a number
*/
#include <stdio.h>
#include <math.h>
int main()
{
double num, root;
/* Get number input from user */
printf("Enter any number: ");
scanf("%lf", &num);
/* Calculate square root of number */
root = sqrt(num);
/* Print output */
printf("Square root of %.2lf : %.2lf", num, root);
return 0;
}
Output 1
Enter any number: 64
Square root of 64.00 = 8.00
Output 1
Enter any number: 59
Square root of 59.00 : 7.68
Find square root of numbers from 1 to 50
Here, we are using the for loop to iterate over 1 to 50 and to get the square root of the number using the sqrt() method.
/**
* C program to get square root of a number
*/
#include<stdio.h>
#include<math.h>
int main()
{
double num, root;
/* Get number input from user */
for(num = 1; num <= 50; num++){
/* Calculate square root of number */
root = sqrt(num);
printf("\nSquare root of %.2lf : %.2lf", num, root);
}
return 0;
}
Output of the above code -
Square root of 1.00 : 1.00
Square root of 2.00 : 1.41
Square root of 3.00 : 1.73
Square root of 4.00 : 2.00
Square root of 5.00 : 2.24
Square root of 6.00 : 2.45
Square root of 7.00 : 2.65
Square root of 8.00 : 2.83
Square root of 9.00 : 3.00
Square root of 10.00 : 3.16
Square root of 11.00 : 3.32
Square root of 12.00 : 3.46
Square root of 13.00 : 3.61
Square root of 14.00 : 3.74
Square root of 15.00 : 3.87
Square root of 16.00 : 4.00
Square root of 17.00 : 4.12
Square root of 18.00 : 4.24
Square root of 19.00 : 4.36
Square root of 20.00 : 4.47
Square root of 21.00 : 4.58
Square root of 22.00 : 4.69
Square root of 23.00 : 4.80
Square root of 24.00 : 4.90
Square root of 25.00 : 5.00
Square root of 26.00 : 5.10
Square root of 27.00 : 5.20
Square root of 28.00 : 5.29
Square root of 29.00 : 5.39
Square root of 30.00 : 5.48
Square root of 31.00 : 5.57
Square root of 32.00 : 5.66
Square root of 33.00 : 5.74
Square root of 34.00 : 5.83
Square root of 35.00 : 5.92
Square root of 36.00 : 6.00
Square root of 37.00 : 6.08
Square root of 38.00 : 6.16
Square root of 39.00 : 6.24
Square root of 40.00 : 6.32
Square root of 41.00 : 6.40
Square root of 42.00 : 6.48
Square root of 43.00 : 6.56
Square root of 44.00 : 6.63
Square root of 45.00 : 6.71
Square root of 46.00 : 6.78
Square root of 47.00 : 6.86
Square root of 48.00 : 6.93
Square root of 49.00 : 7.00
Square root of 50.00 : 7.07
Find square root of numbers using while loop
Here, we are using the while loop to get the square root of the number using the C programming language.
#include <stdio.h>
int main() {
int num;
double start,end,mid;
printf("Enter a number: ");
scanf("%d",&num);
start = 0, end = num;
while((end - start)>=0.000001){
mid = (start + end)/2;
if(mid*mid < num)
start = mid;
if(mid*mid >= num)
end = mid;
}
printf("Square Root of %d is %f",num,mid);
}
Output of the above code:
Enter a number: 12
Square Root of 12 is 3.464102
Enter a number: 19
Square Root of 19 is 4.358899
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative