In this example, You learn about C Program to Find Sum of Each Row in a Matrix.
Here you learn sum of each row in 2d array in c, You are given 2D array and the task of the program to find sum of each row in matrix.
Here you learn sum of each row in 2d array in c, You are given 2D array and the task of the program to find sum of each row in matrix.
C Program to Find Sum of Each Row in a Matrix
#include<stdio.h>
int main()
{
int i,j,sum=0,rows,cols;
printf("Enter Number of rows:");
scanf("%d",&rows);
printf("Enter Number of cols:");
scanf("%d",&cols);
int a[rows][cols];
printf("Enter matrix elements:");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
scanf("%d",&a[i][j]);
}
printf("Matrix elements are:\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
printf("%d ",a[i][j]);
printf("\n");
}
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
sum=sum+a[i][j];
}
printf("\n Sum of %dth row is %d",i,sum);
sum=0;
}
return 0;
}
Enter Number of rows:2
Enter Number of cols:2
Matrix elements are:
4 6
2 4
Sum of 0th row is 10
Sum of 1th row is 6
Happy Coding 😊
Tags:
C