In the previous section we discussed problems related to while loop. Now let us take a look at some more Problems.
1. Inverted Right Angle Triangle
Print the pattern below.
* * * * *
* * * *
* * *
* *
*
#include
int main() {
int i = 5, j;
while (i >= 1) {
j = 1;
while (j <= i) {
printf("*");
j++;
}
printf("\n");
i--;
}
return 0;
}
// *****
// ****
// ***
// **
// *
| Outer Loop (i) | Condition (i >= 1) | Inner Loop (j) | (j <= i) | Stars Printed |
|---|---|---|---|---|
| 5 | True | 1, 2, 3, 4, 5 | True (5 times) | ***** |
| 4 | True | 1, 2, 3, 4 | True (4 times) | **** |
| 3 | True | 1, 2, 3 | True (3 times) | *** |
| 2 | True | 1, 2 | True (2 times) | ** |
| 1 | True | 1 | True (1 times) | * |
| 0 | False | - | - | - |
2. Repeated Number Pattern
1
22
333
4444
55555
#include
int main() {
int i = 1, j;
while (i <= 5) {
j = 1;
while (j <= i) {
printf("%d", i);
j++;
}
printf("\n");
i++;
}
return 0;
}
// 1
// 22
// 333
// 4444
// 55555
| Row (i) | Condition ( i <= 5) | Inner Loop (j) | Inner Condition (j <= i) | Result |
|---|---|---|---|---|
| 1 | True | 1 | 1 <= 1 (True) | 1 |
| 2 | True | 1, 2 | 1,2 <= 2 (True) | 2, 2 |
| 3 | True | 1, 2, 3 | 1, 2, 3 <= 3 (True) | 3, 3, 3 |
| 4 | True | 1, 2, 3, 4 | 1, 2, 3, 4 <= 4(True) | 4, 4, 4, 4 |
| 5 | True | 1, 2, 3, 4, 5 | 1, 2, 3, 4, 5 <= 4(True) | 5, 5, 5, 5, 5 |
| 6 | False | - | - |
3. Alphabet Pattern
A
AB
ABC
ABCD
ABCDE
#include
int main() {
int i = 1, j;
while (i <= 5) {
j = 1;
while (j <= i) {
printf("%c", 'A' + j - 1);
j++;
}
printf("\n");
i++;
}
return 0;
}
// A
// AB
// ABC
// ABCD
// ABCDE
| Row (i) | Inner (j) | Calculation: 'A' + j - 1 | Character Printed |
|---|---|---|---|
| 1 | 1 | 'A' + 1 - 1 = 'A' | A |
| 2 | 1, 2 | 'A'+0, 'A'+1 | A, B |
| 3 | 1, 2, 3 | 'A'+0, 'A'+1, 'A'+2 | A, B, C |
| 4 | 1, 2, 3, 4 | 'A'+0... 'A'+3 | A, B, C, D |
| 5 | 1, 2, 3, 4, 5 | 'A'+0... 'A'+4 | A, B, C, D, E |
4. Floyd’s Triangle
1
2 3
4 5 6
7 8 9 10
#include
int main() {
int i = 1, j, num = 1;
while (i <= 4) {
j = 1;
while (j <= i) {
printf("%d ", num);
num++;
j++;
}
printf("\n");
i++;
}
return 0;
}
// 1
// 2 3
// 4 5 6
// 7 8 9 10
| Row (i) | Condition ( i <= 4) | Inner Loop (j) | Value of num | Output |
|---|---|---|---|---|
| 1 | True | 1 | 1 - > 2 | 1 |
| 2 | True | 1, 2 | 2 -> 3 -> 4 | 2 3 |
| 3 | True | 1, 2, 3 | 4 -> 5 -> 6 -> 7 | 4 5 6 |
| 4 | True | 1, 2, 3, 4 | 7 -> 8 -> 9 - > 10 | 7 8 9 10 |
| 5 | False | - | - |
5. Hollow Square
*****
* *
* *
* *
*****
#include
int main() {
int i = 1, j;
while (i <= 5) {
j = 1;
while (j <= 5) {
if (i == 1 || i == 5 || j == 1 || j == 5)
printf("*");
else
printf(" ");
j++;
}
printf("\n");
i++;
}
return 0;
}
// *****
// * *
// * *
// * *
// *****
| Row (i) | Row Type | Logic (Why?) | Visual Output |
|---|---|---|---|
| 1 | Top Boundary | i = 1 is True for all 5 stars | ***** |
| 2 | Middle Section | Only j=1 and j=5 are True | * ___* |
| 3 | Middle Section | Only j=1 and j=5 are True | *____* |
| 4 | Middle Section | Only j=1 and j=5 are True | *____* |
| 5 | Bottom Boundary | i = 5 is True for all 5 stars | ***** |