C Program to input 3×3 matrix and print sum of all elements

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:

  1. Multidimensional array
  2. For Loop

Program

#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

Resources

Here are the files you can use to practice on your own and run on your own PC:

Related Programs

  1. C Program to check whether the person is eligible to vote or not.
  2. C Program for Moving Car Animation Using C Graphics
  3. C Program to Draw Pie Chart Using C Graphics
  4. C Program to print multiplication table of a given number.
  5. C Program to concatenate two Strings using pointer

Similar Posts