Home C Programming Language / Programs / Logic to print the given number pattern 11111 2222 333 44 5
🚀 Programming Example

Logic to print the given number pattern
11111
 2222
  333
   44
    5

👁 2,537 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=1; i<=N; i++)
    {
        // Logic to print spaces
        for(j=1; j<i; j++)
        {
            printf(" ");
        }

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

        printf("\n");
    }

    return 0;
}
                        

🖥 Program Output

11111
 2222
  333
   44
    5
                            

📘 Explanation

Logic to print the given number pattern

Now, once you get the logic of printing the previous number pattern you can easily print this pattern. As both are similar except that it contains extra trailing spaces. Hence you only need to add the logic of printing spaces before the number gets printed in the first number pattern. If you point your mouse over the pattern you can actually count total number of spaces per row and can get the logic in which spaces are printed. Now as you can see, each row contains row_number - 1 spaces. Logic to print spaces is-

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

Lets now write down its 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.