C Program to Find Sum of Each Row and Column of a Matrix

Howdy readers, today you will learn how to find the sum of each row and column of a matrix using C Programming language.

C Program to Find Sum of Each Row and Column of a Matrix

This program asks the user to enter the no. of rows and columns of the matrix. Then, it asks the user to enter the elements of the matrix.

Suppose, we have a matrix A such that:

A = 1 2 3
    4 5 6
    7 8 9

Then, sum of first row will be: 1 + 2 + 3 = 6.
Sum of first column will be: 1 + 4 + 7 = 12.

We will use a for loop to compute the sum of each row and column of a matrix.

So, without any delay, let’s begin this tutorial.

Table of Contents

C Program to Find Sum of Each Row and Column of a Matrix

C Program

// C Program to Find Sum of Each Row and Column of a Matrix
#include <stdio.h>
int main(){
    int i, j, row, col, sum = 0;
    int a[25][25];
    
    // Asking for input
    printf("Enter the no. of rows: ");
    scanf("%d", &row);
    printf("Enter the no. of columns: ");
    scanf("%d", &col);
    
    // Enter each element of the matrix
    printf("Enter the element of the matrix: \n");
    for (i = 0; i < row; i++){
        for (j = 0; j < col; j++){
            scanf("%d", &a[i][j]);
        }
    }
    
    // Finding sum of rows
    for (i = 0; i < row; i++){
        sum = 0;
        for (j = 0; j < col; j++){
            sum = sum + a[i][j];
        }
        printf("Sum of elements of row %d = %d\n", i+1, sum);
    }
    
    // Finding sum of columns
    for (i = 0; i < row; i++){
        sum = 0;
        for (j = 0; j < col; j++){
            sum = sum + a[j][i];
        }
        printf("Sum of elements of column %d = %d\n", i+1, sum);
    }
    
    return 0;
}

Output

Enter the no. of rows: 3
Enter the no. of columns: 3
Enter the element of the matrix: 
2 7 9
6 4 8
3 5 7
Sum of elements of row 1 = 18
Sum of elements of row 2 = 18
Sum of elements of row 3 = 15
Sum of elements of column 1 = 11
Sum of elements of column 2 = 16
Sum of elements of column 3 = 24
C Program to Find Sum of Each Row and Column of a Matrix

Explanation

    int i, j, row, col, sum = 0;
    int a[25][25];

In this program, we have declared  5 int data type variables named i, j, row, col, sum and one array named a.

    // Asking for input
    printf("Enter the no. of rows: ");
    scanf("%d", &row);
    printf("Enter the no. of columns: ");
    scanf("%d", &col);
    // Enter each element of the matrix
    printf("Enter the element of the matrix: \n");
    for (i = 0; i < row; i++){
        for (j = 0; j < col; j++){
            scanf("%d", &a[i][j]);
        }
    }

Then, the user is asked to enter the order and element of the matrix.

    // Finding sum of rows
    for (i = 0; i < row; i++){
        sum = 0;
        for (j = 0; j < col; j++){
            sum = sum + a[i][j];
        }
        printf("Sum of elements of row %d = %d\n", i+1, sum);
    }

We used a for loop to compute the sum of each row and the sum is displayed on the screen using printf() function.

    // Finding sum of columns
    for (i = 0; i < row; i++){
        sum = 0;
        for (j = 0; j < col; j++){
            sum = sum + a[j][i];
        }
        printf("Sum of elements of column %d = %d\n", i+1, sum);
    }

Similarly, we compute the sum of each column of the matrix.

Conclusion

I hope after going through this tutorial, you understand how to write a program to find the sum of each row and column of a matrix using C Programming language.

If you have any doubt regarding this tutorial, then let us know in the comment section. We will be glad to solve your doubts.

Leave a Comment