C Program to Find Square Root of a Number

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.

Problem Description:

Write a C program to find square root of the number.

Problem Solution

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:

advertisement
advertisement
Method 1: (Using Sqrt() Function)

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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Program/Source Code

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.

  1. /*
  2.  * C program to find the square root of a number using sqrt function
  3.  */
  4. #include <stdio.h>
  5. int main()
  6. {
  7.     double n;
  8.     printf("Enter number: ");
  9.     scanf("%lf",&n);
  10.  
  11.     //Calling sqrt function
  12.     double square_root=sqrt(n);
  13.  
  14.     //Precision upto 2 decimal places
  15.     printf("Number is %0.2lf and its square root is: %0.2lf",n,sqrt(n));
  16.  
  17.     return 0;
  18. }
Program Explanation

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.

Run Time Testcases

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.

advertisement
Enter number: 64
Number is 64 and its square root is: 8

Method 2: (Using Pow Function)

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.

advertisement

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.

Program/Source Code

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.

  1. /*
  2.  * C program to find the square root of a number using pow function
  3.  */
  4. #include <stdio.h>
  5. int main()
  6. {
  7.     double n;
  8.     printf("Enter number: ");
  9.     scanf("%lf",&n);
  10.  
  11.     // Calling power function with power 0.5
  12.     double square_root=pow(n,0.5);
  13.  
  14.     //Precision upto 2 decimal places
  15.     printf("Number is %0.2lf and its square root is: %0.2lf",n,sqrt(n));
  16.  
  17.     return 0;
  18. }
Program Explanation

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.

Run Time Testcases

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

Method 3: (Using Log Values)

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

Program/Source Code

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.

  1. /*
  2.  * C program to find the square root of a number using log values
  3.  */
  4. #include <stdio.h>
  5. int main()
  6. {
  7.     double n;
  8.     printf("Enter number: ");
  9.     scanf("%lf",&n);
  10.  
  11.     //Finding sqrt using the found formula
  12.     double square_root=pow(2,0.5*log(n));
  13.  
  14.     //Precision upto 2 decimal places
  15.     printf("Number is %0.2lf and its square root is: %0.2lf",n,sqrt(n));
  16.  
  17.     return 0;
  18. }
Program Explanation

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.

Run Time Testcases

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

Method 4: (Using Binary Search)

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.

Program/Source Code

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.

  1. /*
  2.  * C program to find the square root of a number using binary search
  3.  */
  4. #include <stdio.h>
  5.  
  6. double sqrt_n(int n)
  7. {
  8.     // low corresponds to low of binary search i.e from where we start searching the element 
  9.     // high corresponds to high of binary search i.e upto where we start searching the element 
  10.     int low = 0, high = n , mid;
  11.  
  12.     // ans is the variable which will store the sqrt of n. 
  13.     double ans;
  14.  
  15.     // While loop finds the integral part of the square root
  16.     while (low <= high)
  17.     {
  18.         mid = (low + high) / 2;
  19.         if (mid * mid == n)
  20.         {
  21.             ans = mid;
  22.             // This means that ans is a perfect square,so break
  23.             break;
  24.         }
  25.         if (mid * mid < n)
  26.         {
  27.             // This if block means that sqrt will be in right part
  28.             // so updating the ans and start variable
  29.             ans=low;
  30.             low = mid + 1;
  31.         }
  32.         else
  33.         {
  34.             // This else block means that sqrt will be in left part
  35.             // so updating the high variable, ans variable will remain same 
  36.             high = mid - 1;
  37.         }
  38.     }
  39.     // To find precision upto 6 decimal places
  40.     float increment_value = 0.1;
  41.     for (int i = 0; i < 6; i++)
  42.     {
  43.         //Incrementing ans until its square is less than equal to entered number
  44.         while (ans * ans <= n)
  45.         {
  46.             ans += increment_value;
  47.         }
  48.         //Decrementing it once because while loop will break when ans is greater than n
  49.         ans -= increment_value;
  50.         //Dividing increment by 10 for next digit of the precision
  51.         increment_value /= 10;
  52.     }
  53.     return ans;
  54. }
  55.  
  56. int main()
  57. {
  58.     int n;
  59.     printf("Enter number: ");
  60.     scanf("%d",&n);
  61.     printf("Square root of number %d is: %lf ",n, sqrt_n(n));
  62.     return 0;
  63. }
Program Explanation

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).

Run Time Testcases

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”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.