Home / Programs / Program to print number pattern 55555 54444 54333 54322 54321
Programming Example

Program to print number pattern
55555
54444
54333
54322
54321

👁 9,772 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 of m rows and n columns using for loop in C programming. Logic to print the given number pattern using for loop in C program.

Program Code

/**
 * C program to print number pattern
* www.atnyla.com
 */

#include <stdio.h>

int main()
{
    int rows, cols, i, j;

    /* Input rows and columns from user
     */
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    printf("Enter number of columns: ");
    scanf("%d", &cols);

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

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

        printf("\n");
    }

    return 0;
}

Output

Enter number of rows: 5
Enter number of columns: 5
55555
54444
54333
54322
54321

Explanation

Required knowledge

Basic C programming, Loop

Logic to print the given patterns

Before all of these patterns confuse you, I recommend you to learn some basics of number pattern logic.

Once you get acquainted with basics of number pattern printing logic. If you look to these pattern carefully you will find that all of them are similar. Hence if you can decode the logic of any rest of them will be easy to code. Now, have take a minute and look to the below pattern carefully.

 

55555
54444
54333
54322
54321

 

If you have looked carefully you can actually divide this pattern in two parts, let me show you.

Now, I hope that logic of whole pattern would have been cleared to you somewhat. If not let me explain the logic to print this pattern.

  1. To iterate through rows, run an outer loop from 1 to rows (where rows is total rows to be printed).
  2. To print first part of pattern, run an inner loop from columns to columns - current_row (where columns is total columns to be printed). Inside this loop print the value of current column.
  3. To print the second part of pattern, run another inner loop from 1 to columns - current_row. Inside this loop print the value of rows - current_row + 1.

Lets implement this...

Note: Logic to all remaining patterns are similar so I won't be explaining logic of remaining three patterns. But source code of all t

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.