Program to display the cube of the number upto given integer in C++

Program in C++ to display the cube of the number up to given an integer

Given: User will input number of terms and we have to print its cube till the given number of terms.

Example:

Input the number of terms : 4                                          

Number is: 1 and its cube is: 1                                  

Number is: 2 and its cube is: 8                                  

Number is: 3 and its cube is: 27                                 

Number is: 4 and its cube is: 64

# Algorithm

  1. Take the number of terms as input from the user.
  2. Start a loop from i = 1 to i <= n.
  3. Inside the loop, calculate the cube of i by multiplying it by itself twice.
  4. Print the number and its cube as the result.

Code:

#include <iostream>
using namespace std;

int main() {
  int i, n, cube;
  cout << "Input the number of terms: ";
  cin >> n;
  for (i = 1; i <= n; i++) {
    cube = i * i * i;
    cout << "Number is: " << i << " and its cube is: " << cube << endl;
  }
}

Output

Input the number of terms: 5
Number is: 1 and its cube is: 1
Number is: 2 and its cube is: 8
Number is: 3 and its cube is: 27
Number is: 4 and its cube is: 64
Number is: 5 and its cube is: 125

Program to display the cube of the number upto given integer in C++