To find sum of rows and columns in a matrix
Method I
//Coming Soon...
Output:
Method I:
Method II:
Method I
#include<stdio.h>
main()
{
int i,j,rows,col,sum_rows,sum_col,k=0,temp;
printf("Enter number of rows and columns of a matrix\n");
scanf("%d %d",&rows,&col);
int a[rows][col],t[col][rows];
//Taking input of matrix
printf("Enter Matrix 1\n");
for(i=0;i<rows;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Given /matrix is\n");
for(i=0;i<rows;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(i=0;i<rows;i++)
{
sum_rows=0;
for(j=0;j<col;j++)
{
sum_rows+=a[i][j];
}
printf("Sum of %d Row is %d\n",k,sum_rows);
k++;
}
//Transpose and applying same logic as above for calculating columns
//Exchanging rows and columns
temp=rows;
rows=col;
col=temp;
//Transpose of matrix
for(i=0;i<rows;i++)
{
for(j=0;j<col;j++)
{
t[i][j]=a[j][i];
}
}
k=0;
for(i=0;i<rows;i++)
{
sum_col=0;
for(j=0;j<col;j++)
{
sum_col+=t[i][j];
}
printf("Sum of %d Column is %d\n",k,sum_col);
k++;
}
}
Method II #include<stdio.h>
main()
{
int i,j,rows,col,sum_rows,sum_col,k=0;
printf("Enter number of rows and columns of a matrix\n");
scanf("%d %d",&rows,&col);
int a[rows][col];
//Taking input of matrix
printf("Enter Matrix 1\n");
for(i=0;i<rows;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Given /matrix is\n");
for(i=0;i<rows;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(i=0;i<rows;i++)
{
sum_rows=0;
for(j=0;j<col;j++)
{
sum_rows+=a[i][j];
}
printf("Sum of %d Row is %d\n",k,sum_rows);
k++;
}
k=0;
for(i=0;i<col;i++)
{
sum_col=0;
for(j=0;j<rows;j++)
{
sum_col+=a[j][i];
}
printf("Sum of %d Column is %d\n",k,sum_col);
k++;
}
}
Explanation://Coming Soon...
Output:
Method I:


