Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

C program to Print Number Pattern 2

by suresh

In this article we will show you, How to 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;
}

OUTPUT

C program to Print Number Pattern 2 1

ANALYSIS

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

C 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;
}

OUTPUT

C program to Print Number Pattern 2 2

Thank You for Visiting Our Blog

Placed Under: C Programming, C Programs

Stay in Touch!

Sign Up to receive Email updates on Latest Tutorials

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

Copyright © 2019 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy