Home »
C programs »
C two-dimensional arrays programs
C Program to read a matrix and find sum, product of all elements of two dimensional (matrix) array
This program will read a matrix and prints sum and product of all elements of the two dimensional array.
C program to read a matrix and find sum, product of all elements of matrix
#include <stdio.h>
#define MAXROW 10
#define MAXCOL 10
int main() {
int matrix[MAXROW][MAXCOL];
int i, j, r, c;
int sum, product;
printf("Enter number of Rows :");
scanf("%d", &r);
printf("Enter number of Cols :");
scanf("%d", &c);
printf("\nEnter matrix elements :\n");
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
printf("Enter element [%d,%d] : ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
/*sum and product of all elements*/
/*initializing sun and product variables*/
sum = 0;
product = 1;
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
sum += matrix[i][j];
product *= matrix[i][j];
}
}
printf("\nSUM of all elements : %d \nProduct of all elements :%d", sum,
product);
return 0;
}
Output
Enter number of Rows :3
Enter number of Cols :3
Enter matrix elements :
Enter element [1,1] : 1
Enter element [1,2] : 1
Enter element [1,3] : 1
Enter element [2,1] : 2
Enter element [2,2] : 2
Enter element [2,3] : 2
Enter element [3,1] : 3
Enter element [3,2] : 3
Enter element [3,3] : 3
SUM of all elements : 18
Product of all elements :216
C Two-dimensional Arrays Programs »