Program to print the given number pattern
    5
   45
  345
 2345
12345

C Programming Language Loop control in C Language (Article) Loop control in C Language (Program)

5018

Program:

/**
 * 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=i; j<=N; j++)
        {
            printf("%d", j);
        }

        printf("\n");
    }

    return 0;
}

Output:

    5
   45
  345
 2345
12345

Explanation:

Logic to print the given number pattern

 

    5
   45
  345
 2345
12345

 

If you look to the above pattern you will find that it is same as the pattern we just printed above except of trailing spaces. Hence, the whole logic of printing the pattern will be same as first pattern, we only need to add the logic to print spaces. If you hover mouse on to the pattern you can see or count total spaces per row. In the given pattern each row contains i - 1 spaces (where i is the current row number). Note that row are in descending order i.e. row1 is 5, row2 is 4 and so on.
Step-by-step descriptive logic:

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

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.