In this tutorial you will learn about the C Program to Find Sum of each and every Row and Column in a Matrix and its application with practical example.
C Program to Find Sum of each and every Row and Column in a Matrix
In this tutorial, we will learn to create a C program that will Find the Sum of each row and column in a Matrix using C programming.
Prerequisites
Before starting with the tutorial, we assume that you are the 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.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
1. Declaring the variables for the program. 2. Taking the input size of matrix. 3. Taking the input elements of matrix. 4. Adding the elements in the rows of matrix<strong>. </strong>5. Adding the elements in the Columns of matrix<strong>.</strong> 6. Printing the result<strong> rows and column values of Matrix</strong>. 7. End Program. |
Program to find the Sum of each and every Row and Column in a Matrix:-
In this tutorial, we will find the Sum of each row and every column in a Matrix. First, we will take the size of the matrix from the user, and then we will take the input elements of the matrices. Now we will use the for loop to add the values of rows of that matrix, as well as adding the elements of the columns. At last, we will print the result matrix row and the column values after adding.
Below is a C program to find the sum of each row and every column in a matrix.
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 |
/* C Program to find Sum of each row and column of a Matrix */ #include<stdio.h> int main()//BODY OD THE MAIN FUNCTION { //DECLARIING THE REQUIRED VARIABLE FOR THE PROGRAM int i, j, RW, CL, a[10][10], Sum; //TAKING THE SIZE OF THE MATRIX . printf("\n Please Enter Number of rows and columns : "); scanf("%d %d", &i, &j); //TAKING THE ELEMENTS OF THE MATRIX. printf("\n Please Enter the Matrix Elements \n"); for(RW = 0; RW < i; RW++) { for(CL = 0; CL < j; CL++) { scanf("%d", &a[RW][CL]); } } //FINDING THE SUM OF ALL THE ROWS AND COLUMNS ELEMENTS for(RW = 0; RW < i; RW++) { Sum = 0; for(CL = 0;CL < j; CL++) { Sum = Sum + a[RW][CL]; } //PRINTING THE SUM OF ELEMENTS OF ROWS IN A MATRIX printf("\n The Sum of Elements of a rows in a Matrix = %d", Sum ); } for(RW = 0; RW < i; RW++) { Sum = 0; for(CL = 0;CL < j; CL++) { Sum = Sum + a[CL][RW]; } //PRINTING THE SUM OF ELEMENTS OF COLUMNS IN A MATRIX printf("\n The Sum of Elements of a columns in a 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.
- rw = it will hold the integer value for the loop.
- cl = it will hold the integer value for the loop.
- row = it will hold the input integer value for rows.
- Column = it will hold the input integer value for columns.
Taking the size and the elements of the matrix from the user.
Calculating the Sum of each ROW and every COLUMN in the matrix using a for loop.
Printing the Sum of each Row in a matrix.
Printing the Sum of every Column in a column.