Home C Programming Language / Programs / Program to print the given number pattern 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
🚀 Programming Example

Program to print the given number pattern
1  2  3  4  5
6  7  8  9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

👁 5,639 Views
💻 Practical Program
📘 Step Learning
/** * C program to print number pattern */ #include int main() { int rows, cols, i, j, k; /* Input rows and columns from user */ printf("Enter number of rows: "); scanf("%d", &rows); printf("Enter number of columns: "); scanf("%d", &cols); k = 1; for(i=1; i<=rows; i++) { for(j=1; j<=cols; j++, k++) { printf("%-3d", k); } printf("\n"); } return 0; }

💻 Program Code

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

#include <stdio.h>

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

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

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

        printf("\n");
    }

    return 0;
}
                        

🖥 Program Output

Enter number of rows: 5
Enter number of columns: 5
1  2  3  4  5
6  7  8  9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
                            

📘 Explanation

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern

Before we get into detail with the current number pattern I recommend you to go through previous number patterns to get basics of printing number pattern.

Now, once you get acquainted with the basics of printing number pattern look to the pattern carefully and you will notice that for each column number gets printed in an incremented fashion. Hence to print this pattern we will use another variable lets say k which will keep track of the number to be printed. Each time the number gets printed we will increment the value of k to get the next number.
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.