C++ Program to Find Cube of a Number

C++ Program to Find Cube of a Number

In C++ programming, one of the simplest but very useful tasks is calculating the cube of a number. The cube of a number is simply that number multiplied by itself three times. For example, the cube of 3 is 27 because 3 × 3 × 3 = 27. Cubes are used often in mathematics, physics, engineering, and programming tasks like volume calculations, formulas, and even in certain algorithms. In this article, we will explore different ways to write a C++ program to find the cube of a number. Each program will be explained step by step in a simple, clear, and engaging way so that beginners can follow along without difficulty.

Program 1: Using Simple Multiplication

The most straightforward way to calculate the cube of a number is by multiplying the number by itself three times. This method is easy to understand and is a great starting point for beginners.

#include <iostream>
using namespace std;

int main() {

    int num, cube;

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

    cube = num * num * num;

    cout << "Cube of " << num << " is " << cube << endl;

    return 0;

}

In this program, the user enters a number, and the program multiplies it by itself three times to get the cube. The result is stored in the variable cube and then displayed on the screen. This method is simple, direct, and perfect for beginners who are just learning how to handle arithmetic operations in C++.

Program 2: Using a Function

Functions allow us to organize code and make it reusable. Instead of writing the cube logic inside the main() function, we can create a separate function for it.

#include <iostream>

using namespace std;

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

int main() {

    int num;

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

    cout << "Cube of " << num << " is " << findCube(num) << endl;

    return 0;

}

In this program, we defined a function called findCube that takes a number as input and returns its cube. The main() function simply calls this helper function and displays the result. This is useful in larger programs where the cube calculation may be needed multiple times, as it avoids repeating the same code. Beginners will also get comfortable with the concept of functions.

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

C++ provides a math library called <cmath>, which includes the pow() function. This function allows us to calculate powers of numbers, including cubes.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    int num;

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

    int cube = pow(num, 3);

    cout << "Cube of " << num << " is " << cube << endl;

    return 0;

}

Here, the program takes a number as input and uses the pow() function, where the first argument is the number and the second argument is 3, representing the power. The result is stored in the variable cube and displayed. This method is flexible because the same function can be used for other powers as well, such as squares, fourth powers, or higher. For beginners, it’s also an introduction to using library functions.

Program 4: Cube of a Predefined Number

At times, you may want to calculate the cube of a number that is already known, without asking the user for input. This is helpful for quick testing or when working with fixed values.

#include <iostream>

using namespace std;

int main() {

    int num = 5;
    int cube = num * num * num;

    cout << "Cube of " << num << " is " << cube << endl;

    return 0;

}

In this program, the number 5 is predefined in the variable num. The cube is calculated by multiplying it by itself three times and stored in the variable cube. This style is good for practice when you want to focus only on the logic without typing input each time you run the program.

Frequently Asked Questions (FAQ)

Let’s answer some common questions that beginners often have while learning how to calculate cubes in C++.

Q1: Which method is the best for beginners?
The multiplication method is the simplest and fastest for beginners. It requires fewer resources and is easy to understand.

Q2: When should I use the pow() function?
Use the pow() function if you want flexibility to calculate higher powers, not just cubes. But for cubes specifically, multiplication is usually more efficient.

Q3: Can cubes be calculated for decimal numbers?
Yes, they can. Instead of int, you can use float or double to handle decimal values and then calculate their cube.

Conclusion

Finding the cube of a number in C++ is a simple yet important exercise for beginners. We looked at four different approaches: direct multiplication, using a separate function, applying the pow() function from <cmath>, and working with predefined values. Each method has its own advantages, and practicing them will not only improve your coding skills but also strengthen your understanding of mathematical operations in programming. Keep experimenting with different numbers, including negative and decimal values, to build your confidence.

Additional & References

If you are learning C++, it’s a good idea to try simple programs like cube, square, and square root calculations. These small exercises build a strong foundation in logic, arithmetic, and function usage. Later, you can apply these same concepts to solve bigger and more complex problems.

Scroll to Top