C Program to Print A Matrix
Two loops in nested forms are required to print a matrix in C. For, while, do-while, or any mix of these loops are all acceptable. But compared to the while and do-while loops, the for loop's syntax is simpler to understand.
Implementation to print a Matrix:-
#include <stdio.h>
#define MAXROW 10
#define MAXCOL 10
int main()
{
int mat[MAXROW][MAXCOL];
int i,j,r,c;
printf("Enter number of Rows :");
scanf("%d",&r);
printf("Enter number of Columns :");
scanf("%d",&c);
printf("\nEnter elements of matrix :\n");
for(i=0;i< r;i++)
{
for(j=0;j< c;j++)
{
printf("Enter element at [%d,%d] : ",i+1,j+1);
scanf("%d",&mat[i][j]);
}
}
printf("\nMatrix is :\n");
for(i=0;i< r;i++)
{
for(j=0;j< c;j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n"); /*new line after row elements*/
}
return 0;
}
Output:-
![C Program to Print A Matrix](https://static.tutorialandexample.com/c/c-program-to-print-a-matrix1.png)