Explore solved C++ while loop exercises, including a multiplication table, algorithm analysis, dry run, complete code, code explanation, and sample output.
We need to write a C++ program that displays a right-angled triangle pattern using nested for loops.
The program should display:
Understanding the problem:
for loop controls the number of rows.for loop controls the number of stars in each row.cout << "* "; displays a star followed by a space.cout << endl; moves the cursor to the next line after completing each row.Expected pattern:
*
* *
* * *
* * * *
* * * * *
i to 1.i <= 5:
j to 1.j <= i:
* .j by 1.i by 1.The outer loop executes 5 times. The inner loop executes according to the current value of the outer loop variable i.
Outer Loop (i) |
Inner Loop (j) |
Number of Stars | Output |
|---|---|---|---|
| 1 | 1 | 1 | * |
| 2 | 1, 2 | 2 | * * |
| 3 | 1, 2, 3 | 3 | * * * |
| 4 | 1, 2, 3, 4 | 4 | * * * * |
| 5 | 1, 2, 3, 4, 5 | 5 | * * * * * |
Loop execution:
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
cout << "* ";
}
cout << endl;
}
return 0;
}
Let’s understand the code step by step.
Step 1: Include the input/output library
#include <iostream>
cout to display output on the screen.Step 2: Use the standard namespace
using namespace std;
cout and endl without writing std:: before them.Step 3: Main function
int main()
{
main() function.Step 4: Outer for loop
for (int i = 1; i <= 5; i++)
int i = 1: Initializes the outer loop variable with 1.i <= 5: The loop continues while i is less than or equal to 5.i++: Increments i by 1 after each iteration.Purpose: Controls the number of rows. The loop executes 5 times, creating 5 rows.
Step 5: Inner for loop
for (int j = 1; j <= i; j++)
int j = 1: Initializes the inner loop variable with 1.j <= i: The loop continues while j is less than or equal to the current value of i.j++: Increments j by 1 after each iteration.Purpose: Controls the number of stars in each row.
Important: Notice that the inner loop condition is j <= i, not j <= 5.
This means:
i = 1, the inner loop executes 1 time.i = 2, the inner loop executes 2 times.i = 3, the inner loop executes 3 times.i = 4, the inner loop executes 4 times.i = 5, the inner loop executes 5 times.Therefore, the number of stars increases in every new row.
Step 6: Display the star
cout << "* ";
i.Step 7: Move to the next line
cout << endl;
Step 8: Return statement
return 0;
Remember:
i = 1.i to 2.Important concept:
In this pattern, the outer loop controls the rows, while the inner loop controls the number of stars in each row based on the current value of the outer loop variable.
*
* *
* * *
* * * *
* * * * *
Try modifying the original program to solve the following exercises.
Modify the program to display a right-angled triangle with 7 rows.
Expected output:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
Modify the program to display a right-angled triangle with only 3 rows.
Expected output:
*
* *
* * *
Replace the star (*) with the hash symbol (#).
Display a triangle with 5 rows.
Expected output:
#
# #
# # #
# # # #
# # # # #
Modify the program to ask the user to enter the number of rows.
Use nested for loops to display the triangle according to the entered number.
Example input:
Enter number of rows: 4
Expected output:
*
* *
* * *
* * * *
Modify the inner loop to display numbers instead of stars.
Each row should display numbers starting from 1 up to the current row number.
Expected output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Modify the program so that each row displays the current outer loop variable i instead of a star.
Expected output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
After completing this example, students should understand:
for loops work.i) in the inner loop condition.Remember:
j <= 5.j <= i.This small difference in the inner loop condition changes the entire pattern!