C program for addition of all elements of matrix

If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().



#include<stdio.h>
main()
{
int i,j,mat[10][10],row,col;
int sum=0;

printf("Enter the number of Rows : \n");
scanf("%d",&row);

printf("\nEnter the number of Columns : \n");
scanf("%d",&col);

//Accepting Elements in Matrix
printf("Enter %d*%d Matrix\n",row,col);
for (i=0; i<row;i++)
    {
      for(j=0;j<col;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];
      }
   }
   //Printing Result
   printf("\nSum of All Elements in Matrix :%d",sum);
}

OUTPUT:

Enter the number of Rows :
3

Enter the number of Columns :
3
Enter 3*3 Matrix
1 5 3
4 5 7
5 7 1

Sum of All Elements in Matrix :38

Leave a Reply

Your email address will not be published. Required fields are marked *

18 + seventeen =

This site uses Akismet to reduce spam. Learn how your comment data is processed.