C Code 39 _ To Find the sum of elements in each row of the given matrix

C Code 39 _ To Find the sum of elements in each row of the given matrix 

If a matrix is a 2x2 matrix then there are two rows .
so find the sum of elements in the two rows and display it.

Sample Input:

1 2 3
4 5 6

Sample Output:

sum of elements of row 1 is 6           //i.e. 1 + 2 + 3
sum of elements of row 2 is 15         //i.e. 4 + 5 + 6

Program:


#include<stdio.h>
int main()
{
int a[50][50];
int s[50];
int i,j,m,n,sum,k=0;
printf("\nEnter no of rows : ");
scanf("%d",&i);
printf("\nEnter no of columns : ");
scanf("%d",&j);
printf("\nEnter %d elements :  \n",i*j);
for(m=0;m<i;m++)
{
for(n=0;n<j;n++)
{
scanf("%d",&a[m][n]);
}
}
for(m=0;m<i;m++)
{
sum=0;
for(n=0;n<j;n++)
{
sum=sum+a[m][n];
}
s[k]=sum;
k=k+1;
}
printf("\n");
for(i=0;i<k;i++)
{
printf("\nSum of row %d is :  %d",i+1,s[i]);
}
return 0;
}


Output:











Thank You ...
Previous Post
Next Post
Related Posts

2 comments:

Popular Posts