Example: If elements of matrix are:
1 2 3
4 5 6
7 8 9
Output:
Sum of row 1 = 6
Sum of row 2 = 15
...
...
Sum of column 3 = 18
Required knowledge:
Basic C programming, For loop, Array, MatrixSum of rows and columns of a matrix can be defined as
Program:
/**
* C program to find sum of elements of rows and columns of matrix
*/
#include <stdio.h>
int main()
{
int A[3][3];
int row, col, sum = 0;
/*
* Reads elements in matrix from user
*/
printf("Enter elements in matrix of size 3x3: \n");
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
scanf("%d", &A[row][col]);
}
}
/*
* Finds the sum of elements of each row of matrix
*/
for(row=0; row<3; row++)
{
sum = 0;
for(col=0; col<3; col++)
{
sum += A[row][col];
}
printf("Sum of elements of Row %d = %d\n", row+1, sum);
}
/*
* Finds the sum of elements of each columns of matrix
*/
for(row=0; row<3; row++)
{
sum = 0;
for(col=0; col<3; col++)
{
sum += A[col][row];
}
printf("Sum of elements of Column %d = %d\n", row+1, sum);
}
return 0;
}
Output
Enter elements in matrix of size 3x3:
1 2 3
4 5 6
7 8 9
Sum of elements of Row 1 = 6
Sum of elements of Row 2 = 15
Sum of elements of Row 3 = 24
Sum of elements of Column 1 = 12
Sum of elements of Column 2 = 15
Sum of elements of Column 3 = 18
1 2 3
4 5 6
7 8 9
Sum of elements of Row 1 = 6
Sum of elements of Row 2 = 15
Sum of elements of Row 3 = 24
Sum of elements of Column 1 = 12
Sum of elements of Column 2 = 15
Sum of elements of Column 3 = 18
Happy coding ;)
You may also like
- Array and Matrix programming exercises index.
- C program to find sum of two matrices.
- C program to find sum of main diagonal elements.
- C program to find sum of opposite diagonal elements.
- C program to find difference of two matrices.
- C program to perform scalar matrix multiplication.
- C program to find transpose of a matrix.
- C program to find determinant of a matrix.
- C program to find maximum between two numbers using switch case.
- C program to create simple calculator using switch case.
- C program to print all Prime numbers between 1 to n.
- C program to print all Armstrong numbers between 1 to n.
- C program to print all Strong numbers between 1 to n.
- C program to print all Perfect numbers between 1 to n.
- C program to print all Prime factors of a number.