Number pattern 45 in C

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

Example:
Input N: 5
Output:
1
22
333
2222
11111


Required knowledge

Basic C programming, Loop

Logic to print the given pattern

1
22
333
2222
11111
The given pattern is very much similar to one of previous explained pattern with little trick. To get the logic of this pattern take a moment and think. If still its difficult to get the logic. Read the below observations about the pattern.
  1. It contains total N rows (where N is the total number of rows to be printed).
  2. Each row contains total i columns (where i is the current row number).
  3. For each row less than N / 2 current row number i.e. i gets printed.
  4. For each row greater than N / 2 a value N - i + 1 gets printed.
Lets, write down code for 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;

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

    for(i=1; i<=N; i++)
    {
        for(j=1; j<=i; j++)
        {
            if(i <= (N/2))
            {
                printf("%d", i);
            }
            else
            {
                printf("%d", (N - i + 1));
            }
        }

        printf("\n");
    }

    return 0;
}


Output
Enter rows: 5
1
22
333
2222
11111


Screenshot

Number pattern in C


Happy coding ;)


You may also like

Labels: , , ,