Input numbers in 2d array and print the largest number in each row in C
Two Dimensional Array - Question 9
In this question, we will see how to input numbers in a 3X3 integer matrix (2d array) and print the largest number in each row in C programming. To know more about two dimensional array click on the two dimensional array lesson.
Q9) Write a program in C to input numbers in a 3X3 integer matrix (2d array) and print the largest number in each row.
Program
#include <stdio.h>
#include <conio.h>
int main()
{
int a[3][3], r,c,lg,x=0;
printf("Enter 9 numbers\n");
for(r=0; r<3; r++)
{
for(c=0; c<3; c++)
{
scanf("%d",&a[r][c]);
}
}
for(r=0; r<3; r++)
{
x=0;
for(c=0; c<3; c++)
{
printf("%d ",a[r][c]);
if(x==0)
{
lg=a[r][c];
x=1;
}
else if(a[r][c]>lg)
{
lg=a[r][c];
}
}
printf(" Largest Number = %d\n",lg);
}
return 0;
}
Output
Enter 9 numbers 18 12 5 10 15 45 38 72 64 18 12 5 Largest Number = 18 10 15 45 Largest Number = 45 38 72 64 Largest Number = 72