Square Root Program in C: The square root of a number is defined as the value, which gives the number when it is multiplied by itself i.e half power of the number. The radical symbol √ is used to indicate the square root.
Examples:
If the number is 25, its square root is 5.
If the number is 10, its square root is 3.162.
Write a C program to find square root of the number.
1. Take a number as input.
2. Calculate the square root of the number using sqrt() function.
3. Print the final result and exit.
Square Root of a Number in C can be found out in following ways:
- Square Root Program in C using Sqrt Function
- Square Root Program in C using Pow Function
- Square Root Program in C using Log Values
- Square Root Program in C using Binary Search
Sqrt() is an inbuilt function which takes a double value as a parameter and returns the square root of that double value.
Function Prototype: double sqrt(double num);
Return value: It returns the square root of the number which is a double value.
Examples:
Let x = 25, so sqrt(x) = sqrt(25) = 5.
Let x = 64, so sqrt(x) = sqrt(64) = 8.
Here is the source code of the C program to find the square root of a number using sqrt function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to find the square root of a number using sqrt function
*/
#include <stdio.h>
int main()
{
double n;
printf("Enter number: ");
scanf("%lf",&n);
//Calling sqrt function
double square_root=sqrt(n);
//Precision upto 2 decimal places
printf("Number is %0.2lf and its square root is: %0.2lf",n,sqrt(n));
return 0;
}
1. Input the number whose square root has to be found.
2. Call sqrt() function and store its value.
3. Print value returned by sqrt() which is the square root of the number.
Testcase 1: In this case, we enter the value “25” as input to find the square root.
Enter number: 25 Number is 25 and its square root is: 5
Testcase 2: In this case, we enter the value “64” as input to find the square root.
Enter number: 64 Number is 64 and its square root is: 8
pow() is an inbuilt function which takes a double value and its power as two parameters and returns the power of that number.When power parameter of pow() function is 0.5, it will return square root of the number.
Function Prototype: double pow(double num,double x);
Return value: It returns the value (num)x.
Examples:
Let x = 25, so sqrt_x = pow(x,0.5) = pow(25,0.5) = 5.
Let x = 16, so sqrt_x = pow(x,0.5) = pow(16,0.5) = 4.
Here is the source code of the C program to find the square root of a number using pow function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to find the square root of a number using pow function
*/
#include <stdio.h>
int main()
{
double n;
printf("Enter number: ");
scanf("%lf",&n);
// Calling power function with power 0.5
double square_root=pow(n,0.5);
//Precision upto 2 decimal places
printf("Number is %0.2lf and its square root is: %0.2lf",n,sqrt(n));
return 0;
}
1. First input the number, whose square root has to be found.
2. Call the pow() function with power parameter being 0.5 and store its value.
3. Print the value returned by the pow() function which is the square root of the number and the precision is upto 2 decimal places.
Testcase 1: In this case, we enter the value “25” as input to find the square root.
Enter number: 25 Number is 25.00 and its square root is: 5.00
Testcase 2: In this case, we enter the value “16” as input to find the square root.
Enter number: 16 Number is 16.00 and its square root is: 4.00
Using the log of the number, its power can be calculated as follows:
Let a be the square root of the number, So:
a = (n)1/2 Taking log on both sides (base of log is 2) log(a) = 1/2 * (log(n)) a = 2(1/2 * (log(n))) So, a = pow(2,0.5*log(n))
Examples:
Let x = 9, so sqrt_x = pow(2, 0.5*log(x)) = pow(2,0.5*(log(9)) = 3
Let x = 4, so sqrt_x = pow(2, 0.5*log(x)) = pow(2,0.5*(log(4)) = 2
Here is the source code of the C program to find the square root of a number using log values. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to find the square root of a number using log values
*/
#include <stdio.h>
int main()
{
double n;
printf("Enter number: ");
scanf("%lf",&n);
//Finding sqrt using the found formula
double square_root=pow(2,0.5*log(n));
//Precision upto 2 decimal places
printf("Number is %0.2lf and its square root is: %0.2lf",n,sqrt(n));
return 0;
}
1. First input the number, whose square root has to be found.
2. call the pow() function with power parameter being number parameter as 2 and power parameter being (0.5*log(n)) and store its value.
3. Print the value returned by the pow() function which is the square root of the number.
Testcase 1: In this case, we enter the value “9” as input to find the square root.
Enter number: 9 Number is 9.00 and its square root is: 3.00
Testcase 2: In this case, we enter the value “4” as input to find the square root.
Enter number: 4 Number is 4.00 and its square root is: 2.00
In this method, we use the ideology of binary search to find the square root of any number. We use the binary search to find the integral part and then find the decimal part using the iterative statements.
Example:
Let x = 24, whose square root is to be found.
First Finding out the integral part of square root
In the first iteration, low: 0; high: 24; mid: 12. Since mid*mid > n, So looking in right part.
In the second iteration, low: 0; high: 11; mid: 5. Since mid*mid > n, So looking in right part.
In the third iteration, low: 0; high: 4; mid: 2. Since mid*mid < n, So looking in right part.
In the fourth iteration, low: 3; high: 4; mid: 3. Since mid*mid < n, So looking in right part.
In the fifth iteration, low: 4; high: 4; mid: 4. Now mid*mid < n, but low=high, So integral part is 4.
So integral part of 24 is: 4;
Now finding the Decimal Part
After each iteration, ans value is: 4.8
After each iteration, ans value is: 4.89
After each iteration, ans value is: 4.898
After each iteration, ans value is: 4.8989
After each iteration, ans value is: 4.89897
After each iteration, ans value is: 4.898979
Square root of number 24 is: 4.898979
Algorithm:
Step 1: Start the program.
Step 2: Input the number whose square root has to be found.
Step 3: Declare the function to find square root. Inside the function:
- Find the integral part of the square root using binary search and store it in ans.
- Increment ans by increment_value until it’s less than equal to number, then decrement it once and move to the next precision digit.
- Repeat the above step for all precision digit.
- Return the square root.
Step 4: Print value of square root.
Step 5: End the Program.
Here is the source code of the C program to find the square root of a number using binary search. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to find the square root of a number using binary search
*/
#include <stdio.h>
double sqrt_n(int n)
{
// low corresponds to low of binary search i.e from where we start searching the element
// high corresponds to high of binary search i.e upto where we start searching the element
int low = 0, high = n , mid;
// ans is the variable which will store the sqrt of n.
double ans;
// While loop finds the integral part of the square root
while (low <= high)
{
mid = (low + high) / 2;
if (mid * mid == n)
{
ans = mid;
// This means that ans is a perfect square,so break
break;
}
if (mid * mid < n)
{
// This if block means that sqrt will be in right part
// so updating the ans and start variable
ans=low;
low = mid + 1;
}
else
{
// This else block means that sqrt will be in left part
// so updating the high variable, ans variable will remain same
high = mid - 1;
}
}
// To find precision upto 6 decimal places
float increment_value = 0.1;
for (int i = 0; i < 6; i++)
{
//Incrementing ans until its square is less than equal to entered number
while (ans * ans <= n)
{
ans += increment_value;
}
//Decrementing it once because while loop will break when ans is greater than n
ans -= increment_value;
//Dividing increment by 10 for next digit of the precision
increment_value /= 10;
}
return ans;
}
int main()
{
int n;
printf("Enter number: ");
scanf("%d",&n);
printf("Square root of number %d is: %lf ",n, sqrt_n(n));
return 0;
}
1. First input the number, then declare function sqrt_n to find the square root of the function.
2. In this function, first find the integral part of the square root using the binary search ideology (ans variable contains integral part initially).
3. Then find the decimal part using for and while loop, for loop is used for the number of precision digits.
4. For each precision digit, first increment the value and compare with the number. Repeat this step until ans is less than or equal to n.
4. Then we decrement the ans (because the while loop will stop when ans is greater than n). Dividing the increment value by 10 (to move to the next precision digit).
5. Then return the square root and print it in the main function.
Time complexity: O(log(n))
Time required for finding the integral part is O(log(n)) and is nearly constant for the precision part, So time complexity is O(log(n)).
Space complexity: O(1)
Since no auxiliary space is required, so space complexity is O(1).
In this case, we enter the value “24” as input to find the square root.
Enter number: 24 Square root of number 24 is: 4.898979
To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.
- Practice BCA MCQs
- Check C Books
- Apply for C Internship
- Practice Computer Science MCQs
- Check Computer Science Books