C++ Program to Find Square Root of a Number
You can find the square root in C++ using the
sqrt() function from the cmath library like this: double root = sqrt(number);.Examples
Input16
OutputSquare root of 16 is 4
Input25
OutputSquare root of 25 is 5
Input0
OutputSquare root of 0 is 0
How to Think About It
To find the square root of a number, you first get the number from the user. Then use the built-in
sqrt() function which calculates the square root. Finally, print the result. This approach is simple and uses the standard math library.Algorithm
1
Get the input number from the user.2
Use the <code>sqrt()</code> function to calculate the square root.3
Display the result to the user.Code
cpp
#include <iostream> #include <cmath> int main() { double number; std::cout << "Enter a number: "; std::cin >> number; double root = sqrt(number); std::cout << "Square root of " << number << " is " << root << std::endl; return 0; }
Output
Enter a number: 16
Square root of 16 is 4
Dry Run
Let's trace the input 16 through the code
1
Input
User enters 16, so number = 16
2
Calculate square root
root = sqrt(16) which equals 4
3
Output
Prints: Square root of 16 is 4
| number | root |
|---|---|
| 16 | 4 |
Why This Works
Step 1: Include cmath library
The cmath library provides the sqrt() function to calculate square roots.
Step 2: Read input
We get the number from the user using std::cin.
Step 3: Calculate and print
We call sqrt(number) to get the square root and print it with std::cout.
Alternative Approaches
Using pow() function
cpp
#include <iostream> #include <cmath> int main() { double number; std::cout << "Enter a number: "; std::cin >> number; double root = pow(number, 0.5); std::cout << "Square root of " << number << " is " << root << std::endl; return 0; }
Uses <code>pow()</code> with exponent 0.5 instead of <code>sqrt()</code>; slightly less direct but works the same.
Manual approximation (Newton's method)
cpp
#include <iostream> int main() { double number, guess, epsilon = 0.00001; std::cout << "Enter a number: "; std::cin >> number; guess = number / 2; while ((guess * guess - number) > epsilon || (number - guess * guess) > epsilon) { guess = (guess + number / guess) / 2; } std::cout << "Square root of " << number << " is " << guess << std::endl; return 0; }
Calculates square root manually using Newton's method; useful if math library is unavailable but more complex.
Complexity: O(1) time, O(1) space
Time Complexity
The sqrt() function runs in constant time as it uses hardware or optimized algorithms.
Space Complexity
Only a few variables are used, so space is constant.
Which Approach is Fastest?
Using sqrt() is fastest and simplest; manual methods are slower and more complex.
| Approach | Time | Space | Best For |
|---|---|---|---|
| sqrt() function | O(1) | O(1) | Simple and fast calculation |
| pow() function | O(1) | O(1) | When exponentiation is needed |
| Newton's method | O(log n) | O(1) | Learning algorithm or no math library |
Always include
cmath and use sqrt() for simple and accurate square root calculation.Forgetting to include
cmath or using integer types which can cause incorrect results.