C Program to Print Pattern Using While Loop: Step-by-Step Guide

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 <stdio.h>

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
5True1, 2, 3, 4, 5True (5 times)*****
4True1, 2, 3, 4True (4 times)****
3True1, 2, 3True (3 times)***
2True1, 2True (2 times)**
1True1True (1 times)*
0False---

2. Repeated Number Pattern

1

22

333

4444

55555

				
					#include <stdio.h>

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
1True11 <= 1 (True)1
2True1, 21,2 <= 2 (True)2, 2
3True1, 2, 31, 2, 3 <= 3 (True)3, 3, 3
4True1, 2, 3, 41, 2, 3, 4 <= 4(True)4, 4, 4, 4
5True1, 2, 3, 4, 51, 2, 3, 4, 5 <= 4(True)5, 5, 5, 5, 5
6False--

3. Alphabet Pattern

A

AB

ABC

ABCD

ABCDE

				
					#include <stdio.h>

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 - 1Character Printed
11'A' + 1 - 1 = 'A'A
21, 2'A'+0, 'A'+1A, B
31, 2, 3'A'+0, 'A'+1, 'A'+2A, B, C
41, 2, 3, 4'A'+0... 'A'+3A, B, C, D
51, 2, 3, 4, 5'A'+0... 'A'+4A, B, C, D, E

4. Floyd’s Triangle

1

2 3

4 5 6

7 8 9 10

				
					#include <stdio.h>

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 numOutput
1True11 - > 21
2True1, 22 -> 3 -> 42 3
3True1, 2, 34 -> 5 -> 6 -> 74 5 6
4True1, 2, 3, 47 -> 8 -> 9 - > 107 8 9 10
5False--

5. Hollow Square

*****
*      *
*      *
*      *
*****

				
					#include <stdio.h>

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 TypeLogic (Why?)Visual Output
1Top Boundaryi = 1 is True for all 5 stars*****
2Middle SectionOnly j=1 and j=5 are True* ___*
3Middle SectionOnly j=1 and j=5 are True*____*
4Middle SectionOnly j=1 and j=5 are True*____*
5Bottom Boundaryi = 5 is True for all 5 stars*****