In this tutorial you will learn about the C Program to Sum All Matrix Elements and its application with practical example.
C Program to Print Sum All Matrix Elements
In this tutorial, we will learn to create a C program that will Print Sum All Matrix Elements using C programming.
Prerequisites
Before starting with the tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C programming.
Program to Print Sum of All Matrix Elements
In c programming, it is possible to take a numerical input for the size of the matrix and the elements of the matrix from the user and Print Sum of All Matrix Elements with the help of a very small amount of code. The C language has many types of header libraries which has supported function in them with the help of these files the programming is easy.
Algorithm:-
With the help of this program, we can Print Sum of All Matrix Elements.
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the variables for the program. 2. Taking the input size of matrix. 3. Taking the input elements of matrix. 4. Calculating the<strong> Sum of All Matrix Elements</strong>. 5. Printing the calculated elements. 6. End Program. |
Program:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
/* C Program to find Sum All Matrix Elements */ #include<stdio.h> int main() { /* Declaring the variable for the program for storing the data */ int a[10][10],r,c,sum=0,i,j; //taking the input size of the row from the user printf("/*How Many Rows You Want To \nEnter in Matrix*/\nEnter Limit : "); scanf("%d",&r); //taking the input size of the column from the user printf("\n/*How Many Columns You Want To \nEnter in Matrix*/\nEnter Limit : "); scanf("%d",&c); //taking the elements of matriz in input printf("\nEnter Elements for Matrix of Size %d*%d:\n\n",r,c); for(i=0;i<r;i++) for(j=0;j<c;j++) { scanf("%d",&a[i][j]); } //adding all the elements of the matrix printf("\n%d*%d Matrix : \n\n",r,c); for(i=0;i<r;i++) { for(j=0;j<c;j++) { printf("%2d ",a[i][j]); } printf("\n"); } for(i=0;i<r;i++) for(j=0;j<c;j++) sum=sum+a[i][j]; //prining the result for the sum of all the elements printf("\nSum of All Elements in Matrix = %d",sum); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- a[10][10]= it will hold the integer value.
- r= it will hold the integer value.
- c= it will hold the integer value.
- sum= it will hold the integer value.
- i= it will hold the integer value.
- j= it will hold the integer value.
Taking the elements of the matrix from the user.
Calculating the sun of all the elements of the matrix.
Printing the sum for all the elements of the matrix.