Number pattern 36 in C

Write a C program to print the given number pattern. How to print the given triangular number pattern using for loop in C programming. Logic to print the given number pattern in C program.

Required knowledge

Basic C programming, Loop

Logic to print given pattern

Before I discuss the logic to print the given pattern I recommend you to have a careful eye on to the pattern. Following observations can be made about the pattern.

  1. It contains of N rows (where N is the total rows to be printed).
  2. Each row contains exactly double column as in previous row. And the first row contains 1 column.
  3. As per each column contains an integer from 1 to 9 in increasing order. When the number count reaches 10 it restarts to 1.

Below is the program that converts the above logic to computer code to print the given pattern.

Program to print the given pattern

/**
 * C program to print the given number pattern
 */

#include <stdio.h>

int main()
{
    int i, j, N, colCount, value;

    colCount = 1;
    value = 1;

    printf("Enter rows: ");
    scanf("%d", &N);

    for(i=1; i<=N; i++)
    {
        for(j=1; j<=colCount; j++)
        {
            if(value == 10)
                value = 1; // Restart at 10

            printf("%d ", value);

            value++;
        }

        printf("\n");

        // Increase the total number of columns by 2
        colCount *= 2;
    }

    return 0;
}

Output

Enter rows: 5
1
2 3
4 5 6 7
8 9 1 2 3 4 5 6
7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4

Happy coding 😉