C Program to Find Square Root of a Number
You can find the square root in C using
sqrt() function from math.h library like this: double root = sqrt(number); where number is the input value.Examples
Input16
OutputSquare root of 16 is 4.000000
Input25
OutputSquare root of 25 is 5.000000
Input0
OutputSquare root of 0 is 0.000000
How to Think About It
To find the square root of a number, first get the number from the user. Then use the built-in
sqrt() function which calculates the square root. Finally, print the result. This avoids manual calculation and uses reliable library code.Algorithm
1
Get the input number from the user.2
Include the math library to use the sqrt function.3
Call the sqrt function with the input number.4
Store the result in a variable.5
Print the square root result.Code
c
#include <stdio.h> #include <math.h> int main() { double number, root; printf("Enter a number: "); scanf("%lf", &number); root = sqrt(number); printf("Square root of %.2lf is %.6lf\n", number, root); return 0; }
Output
Enter a number: 16
Square root of 16.00 is 4.000000
Dry Run
Let's trace input 16 through the code
1
Input number
User enters 16, stored in variable number = 16
2
Calculate square root
Call sqrt(16), which returns 4.0, stored in root
3
Print result
Print 'Square root of 16.00 is 4.000000'
| Step | Variable | Value |
|---|---|---|
| 1 | number | 16 |
| 2 | root | 4.0 |
| 3 | output | Square root of 16.00 is 4.000000 |
Why This Works
Step 1: Include math library
The math.h library provides the sqrt() function to calculate square roots accurately.
Step 2: Use sqrt function
Calling sqrt(number) returns the square root of the input number as a double.
Step 3: Print the result
The program prints the original number and its square root formatted to two decimal places.
Alternative Approaches
Manual approximation using Newton's method
c
#include <stdio.h> int main() { double number, root, guess; printf("Enter a number: "); scanf("%lf", &number); guess = number / 2.0; for (int i = 0; i < 10; i++) { guess = (guess + number / guess) / 2.0; } printf("Approximate square root of %.2lf is %lf\n", number, guess); return 0; }
This method approximates the square root without math library but requires iteration and may be less precise.
Using pow function
c
#include <stdio.h> #include <math.h> int main() { double number, root; printf("Enter a number: "); scanf("%lf", &number); root = pow(number, 0.5); printf("Square root of %.2lf is %lf\n", number, root); return 0; }
Using <code>pow()</code> with exponent 0.5 also calculates square root but is less direct than <code>sqrt()</code>.
Complexity: O(1) time, O(1) space
Time Complexity
The sqrt() function runs in constant time as it uses hardware or optimized library calls.
Space Complexity
Only a few variables are used, so space complexity is constant.
Which Approach is Fastest?
Using sqrt() is fastest and most accurate; manual methods are slower and less precise.
| Approach | Time | Space | Best For |
|---|---|---|---|
| sqrt() function | O(1) | O(1) | Fast and accurate calculation |
| Newton's method | O(n) with iterations | O(1) | Learning algorithm and approximation |
| pow() function | O(1) | O(1) | General power calculations including roots |
Always include
math.h and compile with -lm flag to use math functions like sqrt().Forgetting to link the math library with
-lm during compilation causes linker errors.