Home / Programs / Program to print the given number pattern 5 54 543 5432 54321
Programming Example

Program to print the given number pattern
    5
   54
  543
 5432
54321

👁 7,734 Views
💻 Practical Program
📘 Step by Step Learning
Write a C program to print the given number pattern using loop. How to print the given triangular number pattern using for loop in C programming. Logic to print the given number pattern using for loop in C program.

Program Code

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

#include <stdio.h>

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

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

    for(i=N; i>=1; i--)
    {
        // Logic to print spaces
        for(j=1; j<i; j++)
        {
            printf(" ");
        }

        // Logic to print numbers
        for(j=N; j>=i; j--)
        {
            printf("%d", j);
        }

        printf("\n");
    }

    return 0;
}

Output

    5
   54
  543
 5432
54321

Explanation

Logic to print the given number pattern

If you are done with first pattern, then logic to this wouldn't be much difficult to get. This pattern is almost similar to the previous pattern we just printed, except trailing spaces before the number. Hence, logic to print the pattern will be same as the first pattern, we only need to add the logic of printing spaces. You can hover your mouse cursor to the pattern to see or count the number of space. The pattern consists of i - 1 spaces per row (where i is the current row number). Note that in the given pattern we have assumed that row numbers are ordered descending from N-1.
Step-by-step descriptive logic to print spaces:

  1. To print spaces, run an inner loop from 1 to i - 1. Inside this loop print single blank space.

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.