In this tutorial you will learn about the C Program to Print Sum of Each Row and Column of given Matrix and its application with practical example.
C Program to Print Sum of Each Row and Column of given Matrix
In this tutorial, we will learn to create a C program that will Print Sum of Each Row and the Column of the given Matrix 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 Each Row and the Column of the given Matrix
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 Each Row and the Column of the given Matrix 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 Each Row and the Column of the given Matrix.
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 sum of matrix. 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#include <stdio.h> int main() { //Declaring the variable int m,n; //Input size Row Column //Taking the input size from the user. printf("Enter the number of rows and column\n"); scanf("%d %d",&m,&n); //Row Column Initialization int arr[m][n]; //Matrix Declaration //Taking the elements of matrix from the user printf("Enter the elements of the matrix\n"); for(int i=0;i<m;i++) //Matrix Initialization { for(int j=0;j<n;j++) { scanf("%d",&arr[i][j]); } } ////Print Matrix the elements that are given from the user printf("\nElements in the matrix are \n"); for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { printf("%d ",arr[i][j]); } printf("\n"); } //Calculating the sum of matrix printf("\nRow Sum....\n"); for(int i=0;i<m;i++) { int rsum=0; for(int j=0;j<n;j++) { rsum=rsum+arr[i][j]; } printf("\nSum of all the elements in row %d is %d\n",i,rsum); } printf("\nColumn Sum....\n"); for(int i=0;i<m;i++) { int csum=0; for(int j=0;j<n;j++) { csum=csum+arr[j][i]; } printf("\nSum of all the elements in column %d is %d\n",i,csum); } return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- m= it will hold the integer value.
- n= it will hold the integer value.
- arr[m][n]= it will hold the integer value.
Taking the elements of the matrix from the user.
Printing the input matrix.
Printing the sum for rows and columns.