Home C Programming Language / Programs / Program to print the given pattern 12345 23456 34567 45678 56789
🚀 Programming Example

Program to print the given pattern
12345
23456
34567
45678
56789

👁 37,212 Views
💻 Practical Program
📘 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 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=i; j < i+cols; j++)
        {
            printf("%d", j);
        }

        printf("\n");
    }

    return 0;
}
                        

🖥 Program Output

Enter number of rows: 5
Enter number of columns: 5
12345
23456
34567
45678
56789
                            

📘 Explanation

Required knowledge

Basic C programming, Loop

Logic to print the given pattern

Before moving on to this pattern it is recommended that you get acquainted with logic to print basic number pattern.

Once you get familiar to the number patterns carefully have an eye on this pattern. If you look carefully you will notice that for each column numbers are started with the row number, lets say the starting number in each row is k. For each column numbers get printed till k + columns (where columns is the total number of columns to be printed per row). Hence, we would apply this logic inside our looping constructs to print the given pattern.

📚 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.