C++ Program to Calculate Square Root of a Number

C++ Program to Calculate Square Root of a Number

Finding the square root of a number is a basic yet important mathematical operation. In simple words, the square root of a number is a value that, when multiplied by itself, gives the original number. For example, the square root of 25 is 5, because 5 × 5 = 25. In programming, square roots are used in scientific calculations, engineering problems, geometry, and even computer graphics. In this article, we will explore different ways to calculate the square root of a number in C++ with beginner-friendly examples.

Program 1: Using the sqrt() Function from <cmath>

The simplest and most common way to calculate the square root in C++ is by using the built-in sqrt() function from the <cmath> library. This method is quick, accurate, and beginner-friendly.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double num;

    cout << "Enter a number: " << endl;
    cin >> num;

    double result = sqrt(num);

    cout << "Square root of " << num << " is " << result << endl;

    return 0;

}

In this program, the user enters a number, and the sqrt() function calculates its square root. The result is stored in a variable of type double, which allows handling decimal values. Beginners will find this method very useful because it is straightforward and requires very little code.

Program 2: Using the pow() Function

Another way to calculate the square root in C++ is by using the pow() function from the <cmath> library. Instead of directly using sqrt(), we can raise a number to the power of 1.0 / 2.0 (or 0.5), which is the same as finding its square root.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double num;

    cout << "Enter a number: " << endl;
    cin >> num;

    double result = pow(num, 1.0 / 2.0);

    cout << "Square root of " << num << " is " << result << endl;

    return 0;

}

Here, the program takes a number as input and then uses the pow() function to calculate the square root by raising the number to the power of 1.0 / 2.0 (or 0.5). This method is slightly less direct but useful because the same function can also calculate cubes, fourth roots, or any other power. It introduces beginners to the idea that many problems can be solved in more than one way.

Program 3: Calculating Square Root Without Using Built-in Functions

If you want to understand the logic behind square root calculation, you can try writing a program without using sqrt() or pow(). One simple approach is to use a loop to approximate the square root.

#include <iostream>

using namespace std;

int main() {

    double num;

    cout << "Enter a number: " << endl;
    cin >> num;

    double guess = num / 2;
    double accuracy = 0.0001;

    while ((guess * guess - num) > accuracy || (num - guess * guess) > accuracy) {
        guess = (guess + num / guess) / 2;
    }

    cout << "Square root of " << num << " is approximately " << guess << endl;

    return 0;

}

In this program, we use the Newton-Raphson method, which is an iterative way to approximate the square root. We start with a guess (half of the number) and keep improving it until the guess is close enough to the real square root. This method may seem advanced, but it helps beginners understand that mathematical problems can be solved step by step through logic and approximation.

Program 4: Square Root of a Predefined Number

Sometimes you may not need user input at all. Instead, you can directly calculate the square root of a predefined number in your program. This is helpful for quick testing or when working with fixed values.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double num = 49;

    double result = sqrt(num);

    cout << "Square root of " << num << " is " << result << endl;

    return 0;

}

Here, the number 49 is predefined in the program. The square root is calculated using the sqrt() function, and the result is printed directly. Beginners often use this method when practicing because it saves time by avoiding input.

Frequently Asked Questions (FAQ)

Many learners have common doubts when working with square roots in C++. Here are some quick clarifications.

Q1: Which function should I use—sqrt() or pow()?
For simple square root calculations, sqrt() is faster and clearer. Use pow() if you want flexibility to calculate other powers too.

Q2: Can I find the square root of negative numbers?
By default, both sqrt() and pow() will not handle negative numbers properly and may give errors or undefined results. For such cases, you would need complex number handling.

Q3: Why does the program use double instead of int?
Square roots are often decimal values, and using double ensures accuracy. Using int would cut off the decimal part and give incomplete results.

Conclusion

Calculating the square root of a number in C++ is a simple yet important skill. We explored different methods: using the built-in sqrt() function, applying the pow() function, writing a custom logic with loops, and working with predefined values. Each approach helps beginners understand both simple and advanced ways of solving the same problem. By practicing these methods, you will become more confident in using mathematical functions in C++ and ready to apply them to bigger problems.

Additional & References

If you are new to C++, keep experimenting with small programs like these. Try calculating square roots of decimals, large numbers, or combine square root logic with other programs such as calculating distance or area. With practice, you will see how math and programming work beautifully together.

Scroll to Top