Answer:
The c++ program to display cube of numbers from 1 to 15 is given below.
#include <iostream>
using namespace std;
int main() {
// variables declared and initialized
int num = 15, k, cube;
cout << " Cubes of numbers from 1 to 15 are shown below " << endl;
for( k = 1; k <= num; k++ )
{
// cube is calculated for each value of k and displayed
cube = k * k * k ;
cout << " \t " << cube << endl;
}
return 0;
}
OUTPUT
Cubes of numbers from 1 to 15 are shown below
1
8
27
64
125
216
343
512
729
1000
1331
1728
2197
2744
3375
Explanation:
The variables are declared and initialized for loop, cube and for condition in the loop – k, cube, num respectively.
Inside for loop which executes over k, beginning from 1 to 15, cube of each value of k is calculated and displayed. The loop executes 15 times. Hence, cube for numbers from 1 to 15 is displayed after it is calculated.
for( k = 1; k <= num; k++ )
{
cube = k * k * k ;
cout << " \t " << cube << endl;
}
In the first execution of the loop, k is initialized to 1 and variable cube contains cube of 1. Hence, 1 is displayed on the screen.
In the second execution, k is incremented by 1 and holds the value 2. The variable cube contains cube of 2, which is calculated, and 8 gets displayed on the screen.
After each execution of the loop, value of k is incremented.
After 15 executions, value of k is incremented by 1 and k holds the value 16. This new value is greater than num. Hence, the loop terminates.