Home C Programming Language / 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,334 Views
💻 Practical Program
📘 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;
}
                        

🖥 Program 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.

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.