C program to Print Number Pattern 2

Write a C program to Print Number Pattern 2 with example. Or, C Program to Print repeated Number Pattern.

C program to Print Number Pattern 2 using For Loop

This program allows the user to enter the maximum number of rows and columns he/she want to print as a rectangle. Next, compiler will print the numbers from 1 to user specified columns for each row.

/* C program to Print Number Pattern 2 */

#include<stdio.h>
 
int main()
{
    int i, j, rows, columns;
     
    printf(" \nPlease Enter the Number of Rows : ");
    scanf("%d", &rows);
    
    printf(" \nPlease Enter the Number of Columns : ");
    scanf("%d", &columns);
     
    for(i = 1; i <= rows; i++)
    {
    	for(j = 1; j <= columns; j++)
		{
			printf("%d", j);     	
        }
        printf("\n");
    }
    return 0;
}
C program to Print Number Pattern 2 1

Let us see the Nested for loop

for(i = 1; i <= rows; i++)
{
   	for(j = 1; j <= columns; j++)
	{
		printf("%d", j);     	
        }
        printf("\n");
}

Outer Loop – First Iteration

From the above screenshot you can observe that, The value of i is 9 and the condition (i <= 9) is True. So, it will enter into second for loop

Inner Loop – First Iteration

The j value is 1 and the C Programming condition (1 <= 8) is True. So, it will start executing the statements inside the loop.

printf(" %d", j);

Next, we used the Increment Operator j++ to increment the J value by 1. This will happen until the condition inside the inner for loop fails. Next, iteration will start from beginning until both the Inner Loop and Outer loop conditions fails.

Program to Print Number Pattern 2 using while Loop

In this program we just replaced the For Loop with the While Loop. I suggest you to refer While Loop article to understand the logic.

/* C program to Print Number Pattern 2 */

#include<stdio.h>
 
int main()
{
    int i, j, rows, columns;
    i = 1;
     
    printf(" \nPlease Enter the Number of Rows : ");
    scanf("%d", &rows);
    
    printf(" \nPlease Enter the Number of Columns : ");
    scanf("%d", &columns);
     
    while(i <= rows)
    {
    	j = 1;
    	while(j <= columns)
		{
			printf("%d", j);       
			j++;     	
        }
        i++;
        printf("\n");
    }
    return 0;
}
Please Enter the Number of Rows : 9
 
Please Enter the Number of Columns : 12
123456789101112
123456789101112
123456789101112
123456789101112
123456789101112
123456789101112
123456789101112
123456789101112
123456789101112