C program to read m x n matrix and calculate the Row sum and Column sum of the matrix.
In this example, we read a m x n matrix whose value would be entered by the user then the sum of row and column is calculated. Here in the example we have taken a matrix of order 3 x 2 whose row sum and column sum is displayed in the output screen.
PROGRAM TO READ m x n MATRIX AND CALCULATE THE ROW SUM AND COLUMN SUM OF THE MATRIX:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,m[100][100],row,column,rsum,csum=0;
printf("Enter the row size of the matrix :");
scanf("%d",&row);
printf("Enter the column size of the matrix :");
scanf("%d",&column);
printf("Enter the Matrix : ");
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
scanf("%d",&m[i][j]);
}
}
for(i=0;i<row;i++)
{
rsum=0;
for(j=0;j<column;j++)
{
rsum=rsum+m[i][j];
}
printf("\nSum of row %d : %d",i+1,rsum);
}
for(j=0;j<column;j++)
{
csum=0;
for(i=0;i<row;i++)
{
csum=csum+m[i][j];
}
printf("\nSum of column %d : %d",j+1,csum);
}
getch();
}
OUTPUT TO READ M X N MATRIX AND CALCULATE THE ROW SUM AND COLUMN SUM OF THE MATRIX:
Enter the row size of the matrix :3
Enter the column size of the matrix :2
Enter the Matrix : 12
34
65
7
23
9
Sum of row 1 : 46
Sum of row 2 : 72
Sum of row 3 : 32
Sum of column 1 : 100
Sum of column 2 : 50
NOTE: You Can Comment Your Code if you have solved differently and we will pin it in the comment section. Let's Learn together.
MOTTO:
You Learn, I Learn together We Learn.
Comments
Post a Comment
Please do not spam any link