Logic to print the given number pattern
55555
 4444
  333
   22
    1

C Programming Language / Loop control in C Language

2289

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=N; j>i; j--)
        {
            printf(" ");
        }

        // Logic to print numbers
        for(j=1; j<=i; j++)
        {
            printf("%d", i);
        }

        printf("\n");
    }

    return 0;
}

Output:

55555
 4444
  333
   22
    1

Explanation:

Logic to print the given number pattern

Now, once you got the logic of previous number pattern you can easily get the logic of this pattern. As is it same as pattern 1 just we need to add trailing spaces before the number gets printed. If you point your mouse over the below pattern you can count the number of spaces per row and can easily get the logic in which spaces are printed in the pattern.

Here the spaces are in ascending order i.e. row1 contains 0 spaces, row2 contains 1, row3 contains 2 and so on. Also each row contains current_row_number - 1 spaces. Logic to print spaces inside outer loop is:

  1. To print spaces inside outer loop, run an inner loop from N to current_row_number. Inside this loop print spaces.

Lets, now implement this on code.


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.