Explore solved C++ while loop exercises, including a multiplication table, algorithm analysis, dry run, complete code, code explanation, and sample output.
The task is to write a C++ program that asks the user to enter an integer and displays its multiplication table from 1 to 10 using a while loop.
Input:
Processing:
1.1 after each iteration.10.Output:
Programming concepts required:
while loop*)++)Algorithm
num and i.num.i = 1.i is less than or equal to 10:
num * i.i by 1.i becomes greater than 10.Pseudocode
START
DECLARE num, i
DISPLAY "Enter a number: "
INPUT num
SET i = 1
WHILE i <= 10
result = num * i
DISPLAY num, " x ", i, " = ", result
i = i + 1
END WHILE
STOP
A dry run helps us understand how the while loop executes step by step.
Given input: 7
Initially:
num = 7i = 1| Iteration | num | i | Calculation | Result |
|---|---|---|---|---|
| 1 | 7 | 1 | 7 × 1 | 7 |
| 2 | 7 | 2 | 7 × 2 | 14 |
| 3 | 7 | 3 | 7 × 3 | 21 |
| 4 | 7 | 4 | 7 × 4 | 28 |
| 5 | 7 | 5 | 7 × 5 | 35 |
| 6 | 7 | 6 | 7 × 6 | 42 |
| 7 | 7 | 7 | 7 × 7 | 49 |
| 8 | 7 | 8 | 7 × 8 | 56 |
| 9 | 7 | 9 | 7 × 9 | 63 |
| 10 | 7 | 10 | 7 × 10 | 70 |
What happens after the 10th iteration?
i becomes 11.i <= 10 becomes false.#include <iostream>
using namespace std;
int main()
{
int num, i;
cout << "Enter a number: ";
cin >> num;
i = 1;
while (i <= 10)
{
cout << num << " x " << i
<< " = " << num * i << endl;
i++;
}
return 0;
}
Let’s understand the important parts of the program.
int num, i;
num stores the integer entered by the user.i works as a counter to control the loop.cout << "Enter a number: ";
cin >> num;
The program asks the user to enter an integer and stores it in num.
For example, if the user enters 7, then num = 7.
i = 1;
The counter starts at 1 because we need to display the multiplication table from 1 to 10.
while (i <= 10)
This condition checks whether the counter is less than or equal to 10.
cout << num << " x " << i
<< " = " << num * i << endl;
This statement displays the multiplication expression and calculates its result.
For example:
num = 7i = 37 * 3 = 21Output:
7 x 3 = 21
i++;
The ++ operator increases the value of i by 1.
For example:
i = 3i = 4The loop then checks the condition again.
Important: Without i++, the counter would never increase, and the loop would continue indefinitely (infinite loop).
Enter a number: 7
Multiplication Table of 7
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
To display the heading Multiplication Table of 7, add this statement after reading the input:
cout << "Multiplication Table of " << num << endl;
Now modify the existing program to practise independently.
Modify the program to display the multiplication table from 1 to 15 instead of 1 to 10.
Display the multiplication table in reverse order, from 10 down to 1.
Expected output for input 5:
5 x 10 = 50
5 x 9 = 45
5 x 8 = 40
...
5 x 1 = 5
Modify the program to ask the user for a starting number and an ending number. Display the multiplication table only within that range.