C Program to Print 2D Array Elements

Write a C program to print 2D array elements or two-dimensional array items using for loop. In this c example, the first for loop iterates rows, and the second loop iterates columns. Within the C for loop, we print the 2D array elements, and we insert a new line (printf(“\n”)) for each row iteration.

#include<stdio.h>
 
int main()
{
 	int rows, columns;
	
	int a[2][2] = {{10, 20}, {30, 70}};

	printf("\nPrinting the 2D Array\n");
 	for(int rows = 0; rows < 2; rows++)
  	{
  		for(int columns = 0; columns < 2; columns++)
  		{
  			printf("%d  ", a[rows][columns]);
		}
   		printf("\n");
  	}  	

 	return 0;
} 
C Program to Print 2D Array Elements

This C program allows users to enter rows, columns, and the 2D array elements and prints those two dimensional array items.

#include<stdio.h>
 
int main()
{
 	int i, j, rows, columns;
  
 	printf("\nEnter 2D Array rows and columns =  ");
 	scanf("%d %d", &i, &j);
	
	int a[i][j];

 	printf("\nPlease Enter the 2D Array Elements \n");
 	for(rows = 0; rows < i; rows++)
  	{
   		for(columns = 0; columns < j; columns++)
    	{
      		scanf("%d", &a[rows][columns]);
    	}
  	}
	printf("\nPrinting the 2D Array\n");
 	for(rows = 0; rows < i; rows++)
  	{
  		for(columns = 0;columns < j; columns++)
  		{
  			printf("%d  ", a[rows][columns]);
		}
   		printf("\n");
  	}  	

 	return 0;
} 
Enter 2D Array rows and columns =  3 3

Please Enter the 2D Array Elements 
11 22 33 44 55 66 77 88 99

Printing the 2D Array
11  22  33  
44  55  66  
77  88  99 

C program to print 2D array elements using a while loop

#include <stdio.h>

int main()
{
	int i, j, rows, columns;

	printf("\nEnter 2D Array rows and columns =  ");
	scanf("%d %d", &i, &j);

	int a[i][j];

	printf("\nPlease Enter the 2D Array Elements \n");
	rows = 0;
	while (rows < i)
	{
		columns = 0;
		while (columns < j)
		{
			scanf("%d", &a[rows][columns]);
			columns++;
		}
		rows++;
	}
	printf("\nPrinting the 2D Array\n");
	rows = 0;
	while (rows < i)
	{
		columns = 0;
		while (columns < j)
		{
			printf("%d  ", a[rows][columns]);
			columns++;
		}
		printf("\n");
		rows++;
	}
}
Enter 2D Array rows and columns =  3 4

Please Enter the 2D Array Elements 
10 20 30 40
50 60 70 80
90 11 22 33

Printing the 2D Array
10  20  30  40  
50  60  70  80  
90  11  22  33