Write C Program to find the sum of each row and each column of a matrix in 2d array-99webcode
#include<stdio.h>int main(){ int a[3][3],i,j,sum=0,sr,sc; /*here we need two for loop so we ttake two variable i and j ,to calaculate sum of elemete take sum =0*/ printf("enter elements for 2d array "); for(i=0;i<3;i++) //make i=2 for 2 rows { for(j=0;j<3;j++) //make j=3 coloumns { scanf("%d",&a[i][j]); //accepting the the value for row and colomn } } printf("matrix is :\n");
for(i=0;i<3;i++) //make i=3 for 3 rows { for(j=0;j<3;j++) ////make j=2 for 2 rows { printf("%d\t",a[i][j]);//printing the the value for j row and i colomn //sum=sum+a[i][j]; }
printf("\n"); }
for(i=0;i<3;i++){ sr=sc=0; for (int j=0; j<3; j++) { sr=sr+a[i][j];//calculate sum of rows sc=sc+a[j][i];//calculate sum of rows }printf("\n sumofrows is:%d\n sum of colomn is:%d ",sr,sc);}
//printf("sum of array is:%d",sum);//printing the sum of all elementes of 2d array
}
ouput:
enter elements for 2d array 1
2
3
4
5
67
8
9
4
matrix is :
1 2 3
4 5 67
8 9 4
sumofrows is:6
sum of colomn is:13
sumofrows is:76
sum of colomn is:16
sumofrows is:21
sum of colomn is:74
#include<stdio.h>
int main()
{
int a[3][3],i,j,sum=0,sr,sc; /*here we need two for loop so we ttake two variable i and j ,to
calaculate sum of elemete take sum =0*/
printf("enter elements for 2d array ");
for(i=0;i<3;i++) //make i=2 for 2 rows
{
for(j=0;j<3;j++) //make j=3 coloumns
{
scanf("%d",&a[i][j]); //accepting the the value for row and colomn
}
}
printf("matrix is :\n");
for(i=0;i<3;i++) //make i=3 for 3 rows
{
for(j=0;j<3;j++) ////make j=2 for 2 rows
{
printf("%d\t",a[i][j]);//printing the the value for j row and i colomn
//sum=sum+a[i][j];
}
printf("\n");
}
for(i=0;i<3;i++)
{
sr=sc=0;
for (int j=0; j<3; j++)
{
sr=sr+a[i][j];//calculate sum of rows
sc=sc+a[j][i];//calculate sum of rows
}
printf("\n sumofrows is:%d\n sum of colomn is:%d ",sr,sc);
}
//printf("sum of array is:%d",sum);//printing the sum of all elementes of 2d array
}
ouput:
enter elements for 2d array