C++ Program to Calculate Square of a Number

C++ Program to Calculate Square of a Number

In C++, one of the most common beginner exercises is calculating the square of a number. The square of a number simply means multiplying it by itself. For example, the square of 4 is 16, because 4 × 4 = 16. This concept is not just important for mathematics but is also widely used in programming tasks such as area calculations, algorithms, and even graphics programming. In this article, we will learn different ways to write a C++ program to calculate the square of a number. Each method will be explained in a simple and clear manner so beginners can easily follow along.

Program 1: Using Multiplication

The most direct way to find the square of a number is by multiplying the number by itself. This method is simple and easy to understand, which makes it perfect for beginners.

#include <iostream>

using namespace std;

int main() {

    int num, square;  

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

    square = num * num;  

    cout << "Square of " << num << " is " << square << endl;

    return 0;

}

In this program, the user is asked to enter a number. The program then multiplies the number by itself and stores the result in the variable square. Finally, it prints the result. This is the simplest and most beginner-friendly method to calculate a square in C++.

Program 2: Using a Function

To make the program more structured, we can use a function to calculate the square. Functions help us organize code, make it reusable, and keep the logic neat.

#include <iostream>

using namespace std;

int findSquare(int n) {
    return n * n;  
}

int main() {

    int num; 

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

    cout << "Square of " << num << " is " << findSquare(num) << endl;

    return 0;

}

Here, we created a function called findSquare that takes a number as input and returns its square. In the main function, we call this helper function and display the result. This style is useful when the square calculation is needed multiple times in a larger program because it avoids rewriting the same logic.

Program 3: Using the pow() Function from <cmath>

C++ also provides a built-in mathematical library called <cmath>. With this, we can use the pow() function to calculate powers of numbers, including squares.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    int num; 

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

    int square = pow(num, 2);  

    cout << "Square of " << num << " is " << square << endl;

    return 0;

}

In this program, we used the pow() function, which takes two arguments: the base number and the power. By passing 2 as the second argument, we get the square of the number. This method is especially helpful if you plan to calculate higher powers later, such as cubes or fourth powers, because the same function can be reused.

Program 4: Without User Input (Predefined Value)

Sometimes you might not want to take user input and instead work with predefined values. This approach is good for testing and quick calculations.

#include <iostream>

using namespace std;

int main() {

    int num = 7;  
    int square = num * num;  

    cout << "Square of " << num << " is " << square << endl;

    return 0;

}

Here, we set num to 7 directly in the program. The square is calculated in the same way as before using multiplication. Beginners often find this method useful for practicing logic without needing to type input every time.

Frequently Asked Questions (FAQ)

This section clears up some of the common questions that beginners often have when learning how to calculate the square of a number in C++.

Q1: Which method is the best for beginners?
Using simple multiplication is the easiest and most beginner-friendly method. It is fast and does not need extra libraries or functions.

Q2: When should I use the pow() function?
The pow() function is useful if you want to calculate higher powers, not just squares. But for squares, multiplication is usually faster and simpler.

Q3: Can I calculate squares of decimal numbers?
Yes, you can. Instead of using int, you can use float or double to handle decimal numbers and then calculate their squares.

Conclusion

Calculating the square of a number in C++ is a simple but very useful exercise. We explored four different methods: using direct multiplication, using a function, using the pow() function from <cmath>, and working with predefined values. Each method shows a slightly different approach, giving beginners a chance to practice both logic and coding style. With consistent practice, you will get more comfortable and start applying these techniques to larger problems.

Additional & References

If you are serious about improving your C++ skills, it is a good idea to practice these simple problems in different ways. Try experimenting with both integer and floating-point numbers, and combine the square logic with other programs such as calculating the area of a square.

Scroll to Top