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

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

👁 24,640 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
 * www.atnyla.com
 */

#include <stdio.h>

int main()
{
    int i, j, N;

    printf("Enter N: ");
    scanf("%d", &N);

    for(i=N; i>=1; i--)
    {
        for(j=1; j<=i; j++)
        {
            printf("%d", i);
        }

        printf("\n");
    }

    return 0;

}

Output

Enter N: 5
55555
4444
333
22
1

Explanation

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern 1

Before we discuss about logic of printing these patterns. I would recommend you to check out some similar number patterns. As both of the patterns mentioned in this program are just vertically flipped image of

Now, moving on to the logic of first pattern that we need to print. Have a careful eye on to the below pattern

Logic to print the above pattern:

  1. To iterate through rows, initialize an outer loop from N to 1 (where N is the total number of rows). Note that I have initialized the loop from N to 1 not from 1 to N as the pattern is in descending order hence it will help in the inner loop. As both 1 to N or N to 1 will iterate N times.
  2. To print numbers, initialize an inner loop from 1 to current_row_number (note that the current row number will be like n.., 4, 3...1 in decreasing order). Inside this loop print the value of current_row_number.

And you are done, lets implement this now.

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.