Home / Programs / Logic to print the given number pattern 55555 4444 333 22 1
Programming Example

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

👁 2,328 Views
💻 Practical Program
📘 Step by Step Learning
Write a C program to print the given number pattern using loop. How to print the given number pattern using for loop in C programming. Logic to print the given number pattern using 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=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.

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.