C++ Program to Find Cube of a Number

In this post, we will learn how to find the cube of a number using the C++ Programming language.

When a number is multiplied three times by itself, the product obtained is called the cube of a number. For example: The cube of 2 is 2 x 2 x 2 = 8.

We will calculate the cube of a number using the following approaches:

  1. Using Standard Method
  2. Using Function

So, without further ado, let’s begin this tutorial.

C++ Program to Find Cube of a Number

// C++ Program to Find Cube of a Number
#include <iostream>
using namespace std;

int main(){
    int n, cube;
    
    // Asking for input
    cout << "Enter a number: ";
    cin >> n;
    
    // Finding cube 
    cube = n * n * n;
    
    // Displaying result
    cout << "Cube of " << n << " is: " << cube << endl;
    return 0;
}

Output

Enter a number: 2
Cube of 2 is: 8

How Does This Program Work ?

    int n, cube;

In this program, we have declared two integer data type variables named n and cube.

    // Asking for input
    cout << "Enter a number: ";
    cin >> n;

The input is taken from the user with the help of cin statement. The entered value gets stored in the n named variable.

    // Finding cube 
    cube = n * n * n;

Cube is calculated by multiplying the same number three times.

    // Displaying result
    cout << "Cube of " << n << " is: " << cube << endl;

The cube of the number calculated above is displayed on the screen using the cout statement.

C Program to Find Cube of a Number Using Function

// C Program to Find Cube of a Number Using Function
#include <iostream>
using namespace std;

int cubeNum(int);

int main(){
    int n, cube;
    
    // Asking for input
    cout << "Enter a number: ";
    cin >> n;
    
    // Calling out function 
    cube = cubeNum(n);
    
    // Displaying output
    cout << "Cube of " << n << " is: " << cube << endl;
    return 0;
}

int cubeNum(int x){
    return (x * x * x);
}

Output

Enter a number: 3
Cube of 3 is: 27

Conclusion

I hope after going through this post, you understand how to find the cube of a number using the C++ Programming language.

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to solve your doubts.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *