C Program To Find The Square Root Of A Number
Introduction:
“If the square of the number x is equal is to n then we say that x is the square root of n.” Mathematically it can be represented as, x2=n.
Many functions in the C programming language are available in different header files. The sqrt() function is one such method for finding the square root of a number that may be found in the math.h library.
We can find the square root of a numbers in different ways. Some of them are as follows.
The below program used that calculates the square root from a user-given number is shown below.
#include<stdio.h>
#include<math.h>
int main()
{
double n;
printf("Enter a Number:");
scanf("%lf",&n);
double square_root = sqrt(n);
//displaying the square root of a given number
printf("The square root of the number %f is : %f.",n,square_root);
return 0;
}
OUTPUT:
![C Program To Find The Square Root Of A Number](https://static.tutorialandexample.com/c/c-program-to-find-the-square-root-of-a-number1.png)
Implementing the Square root Program using User-defined function and finding up to two decimal places:
#include<stdio.h>
#include<math.h>
//declaration of user defined function
double Square_Root_Function(double n)
{
int integerPart=1; // denotes the integer part of the last obtained square root of the number
while((integerPart*integerPart) <= n)
{
integerPart++;
}
integerPart--;
double Square_Root;
int d1,d2; // to store decimal digit at first place and decimal digit at second place
for(d1=0;d1<10;d1++)
{
for(d2=0;d2<10;d2++)
{
double temp = integerPart + (d1*(1.0/10)) + (d2*(1.0/100)); // creating a number using the integerPart and the two decimal digits
if((temp*temp) <= n) // applying the condition <= so that we can get the closest square root upto two decimal places
{
Square_Root = temp;
}
}
}
return Square_Root;
}
int main()
{
double n;
printf("Enter a number:");
scanf("%lf",&n);
//displaying the square root of anumber
printf("The square root of the number %f is %f", n, Square_Root_Function(n));
return 0;
}
OUTPUT:
![C Program To Find The Square Root Of A Number](https://static.tutorialandexample.com/c/c-program-to-find-the-square-root-of-a-number2.png)
Implementing the square root of number pow() function:
The first parameter to the power function is the number from which the power is to be determined, and the second argument is the number that indicates the power that is to be raised.
We are aware that the square root of an integer is equal to the power of that number raised to 0.5. Consequently, we may use 0.5 as the power function's second argument to determine the number's square root.
The pow() function's code for finding a number's square root is provided below.
#include<stdio.h> #include<math.h> int main() { double n; printf("Enter a Number:"); scanf("%lf",&n); double Square_Root = pow(n,0.5); // displays the square root of a number printf("The square root of the given number %f is : %f.",n,Square_Root); return 0; }
OUTPUT:
![C Program To Find The Square Root Of A Number](https://static.tutorialandexample.com/c/c-program-to-find-the-square-root-of-a-number3.png)
Implementing the square root of a number using log2() function:
We must establish a mathematical connection between the sqrt and log2 functions before we can calculate a number's square root.
We understand that a number 'x' is said to be the square root of a number 'n' if,
X2=n.
#include<stdio.h> #include<math.h> int main() { double n; printf("Enter a number:"); scanf("%lf",&n); double square_Root = pow(2,0.5*log2(n)); printf("The square root of the number %f is %f.",n,square_Root); // prints "The square root of the number 12.000000 is 3.464102." return 0; }
OUTPUT:
![C Program To Find The Square Root Of A Number](https://static.tutorialandexample.com/c/c-program-to-find-the-square-root-of-a-number4.png)
Implementing a square root program using loops:
#include<stdio.h>
#include<math.h>
int main()
{
double n = 25;
int integer_part=1; // denotes the integer part of the final square root of the number
while((integer_part*integer_part) <= n)
{
integer_part++;
}
integer_part--;
double square_root;
int d1,d2; // to store decimal digit at first place and decimal digit at second place
for(d1=0;d1<10;d1++)
{
for(d2=0;d2<10;d2++)
{
double temp = integer_part + (d1*(1.0/10)) + (d2*(1.0/100)); //With the integerPart and the two digits of the decimal, create a number.
if((temp*temp) <= n) //In order to obtain the closest square root to two decimal places, we must use the condition <=.
{
square_root = temp;
}
}
}
//displays the square root of a number
printf("The square root of the number %f is %f", n, square_root);
return 0;
}
OUTPUT:
![C Program To Find The Square Root Of A Number](https://static.tutorialandexample.com/c/c-program-to-find-the-square-root-of-a-number5.png)
Conlusion:
- If a number 'x' and 'n' are equal and the square of that number 'x' is equal to 'n,' then that number is known as the square root of 'n.
- The sqrt function (sqrt()) in the C programming language may calculate a number's square root.
- Various functions, including the power function (pow()) and the log function (log2()), can be used to get the square root of a number.