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.

Input N: 5

Required knowledge

Basic C programming, Loop

Logic to print the given pattern

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);
                printf("%d", (N - i + 1));


    return 0;

Enter rows: 5


Number pattern in C

Happy coding ;)

Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

No comments:

Post a Comment