Home / Programs / Program to print the given pattern 12345 12345 12345 12345 12345
Programming Example

Program to print the given pattern
12345
12345
12345
12345
12345

👁 16,986 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 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=1; j<=cols; j++)
        {
            // Print the current column number
            printf("%d", j);
        }

        printf("\n");
    }

    return 0;
}

Output

Enter number of rows: 5
Enter number of columns: 5
12345
12345
12345
12345
12345

Explanation

Required knowledge

Basic C programming, Loop

Logic to print the given number pattern

Below is the step by step descriptive logic to print the given number pattern.

  1. Input number of rows and columns to print from user. Store it in some variable say rows and cols.
  2. Run an outer loop from 1 to rows. The loop structure should look like for(i=1; i<=rows; i++).
  3. Run an inner loop from 1 to cols. The loop structure should look like for(j=1; j<=cols; j++).
  4. Inside the inner loop print the current column number which is represented by j.
  5. Finally, after printing all columns of a row move to next line.

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.