Addition of All Elements in Matrix


Levels of difficulty: / perform operation:

C Program

#include<stdio.h>
int main() {
	int i, j, mat[10][10], row, col;
	int sum = 0;
	printf("\nEnter the number of Rows : ");
	scanf("%d", &row);
	printf("\nEnter the number of Columns : ");
	scanf("%d", &col);
	//Accept the Elements in Matrix
	for (i = 0; i < row; i++) {
		for (j = 0; j < col; j++) {
			printf("\nEnter the Element mat[%d][%d] : ", i, j);
			scanf("%d", &mat[i][j]);
		}
	}
	//Addition of all Elements
	for (i = 0; i < row; i++) {
		for (j = 0; j < col; j++) {
			sum = sum + mat[i][j];
		}
	}
	//Print out the Result
	printf("\nSum of All Elements in Matrix : %d", sum);
	return (0);
}

Result

Enter the number of Rows : 2
Enter the number of Columns : 2
 
Enter the Element mat[0][0] : 1
Enter the Element mat[0][1] : 1
Enter the Element mat[1][0] : 1
Enter the Element mat[1][1] : 1
 
Sum of All Elements in Matrix : 4





Quantitative Aptitude
Reasoning
Programming
Interview