This C Program to input 3×3 matrix and print sum of all elements. For example, for a 3 x 3 matrix, the sum of all elements of the matrix {1,2,3,4,5,6,7,8,9} will be equal to 45.
Program Outline
We will be writing a C Program to input 3×3 matrix and print sum of all elements. We will be creating a 3 x 3 multidimensional array for storing your array. Now, you need to take values of matrix as input from user.
Concepts
Here are the concepts you need to learn to write this program:
- Multidimensional array
- For Loop
Program
Here’s the C Program to input 3×3 matrix and print sum of all elements:
#include<stdio.h>
#include<conio.h>
int main()
{
int mat[3][3], i, j, sum;
sum = 0;
printf("Enter all 9 elements of 3*3 Matrix:-\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d", &mat[i][j]);
}
}
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
sum = sum + mat[i][j];
}
}
printf("\nSum of all elements = %d", sum);
getch();
}
Output
Here’s the output of C Program to input 3×3 matrix and print sum of all elements:
