Matrix row sum and column sum using C program

In C programming, calculating the sum of rows and columns in a matrix is a fundamental operation. This involves iterating through each row to compute row sums and through each column to compute column sums using nested loops.

Syntax

for(i=0; i<rows; i++) {
    for(j=0; j<cols; j++) {
        rowSum += matrix[i][j];  // Row sum
    }
}

for(j=0; j<cols; j++) {
    for(i=0; i<rows; i++) {
        colSum += matrix[i][j];  // Column sum
    }
}

Method 1: Fixed Input Matrix

This approach uses a predefined matrix to demonstrate row and column sum calculation −

#include <stdio.h>

int main() {
    int matrix[3][3] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    int i, j, rowSum, colSum;
    
    printf("Matrix:<br>");
    for(i = 0; i < 3; i++) {
        for(j = 0; j < 3; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("<br>");
    }
    
    // Calculate row sums
    printf("\nRow Sums:<br>");
    for(i = 0; i < 3; i++) {
        rowSum = 0;
        for(j = 0; j < 3; j++) {
            rowSum += matrix[i][j];
        }
        printf("Row %d: %d<br>", i, rowSum);
    }
    
    // Calculate column sums
    printf("\nColumn Sums:<br>");
    for(j = 0; j < 3; j++) {
        colSum = 0;
        for(i = 0; i < 3; i++) {
            colSum += matrix[i][j];
        }
        printf("Column %d: %d<br>", j, colSum);
    }
    
    return 0;
}
Matrix:
1 2 3 
4 5 6 
7 8 9 

Row Sums:
Row 0: 6
Row 1: 15
Row 2: 24

Column Sums:
Column 0: 12
Column 1: 15
Column 2: 18

Method 2: User Input Matrix

This approach allows users to input matrix elements at runtime −

#include <stdio.h>

int main() {
    int matrix[2][2];
    int i, j, rowSum, colSum;
    
    // Input matrix elements
    printf("Enter elements for 2x2 matrix:<br>");
    for(i = 0; i < 2; i++) {
        for(j = 0; j < 2; j++) {
            printf("Enter element [%d][%d]: ", i, j);
            scanf("%d", &matrix[i][j]);
        }
    }
    
    // Display matrix
    printf("\nMatrix:<br>");
    for(i = 0; i < 2; i++) {
        for(j = 0; j < 2; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("<br>");
    }
    
    // Calculate and display row sums
    printf("\nRow Sums:<br>");
    for(i = 0; i < 2; i++) {
        rowSum = 0;
        for(j = 0; j < 2; j++) {
            rowSum += matrix[i][j];
        }
        printf("Row %d sum = %d<br>", i, rowSum);
    }
    
    // Calculate and display column sums
    printf("\nColumn Sums:<br>");
    for(j = 0; j < 2; j++) {
        colSum = 0;
        for(i = 0; i < 2; i++) {
            colSum += matrix[i][j];
        }
        printf("Column %d sum = %d<br>", j, colSum);
    }
    
    return 0;
}
Enter elements for 2x2 matrix:
Enter element [0][0]: 10
Enter element [0][1]: 20
Enter element [1][0]: 30
Enter element [1][1]: 40

Matrix:
10 20 
30 40 

Row Sums:
Row 0 sum = 30
Row 1 sum = 70

Column Sums:
Column 0 sum = 40
Column 1 sum = 60

Key Points

  • For row sum: outer loop iterates through rows, inner loop through columns
  • For column sum: outer loop iterates through columns, inner loop through rows
  • Always reset the sum variable to 0 for each new row or column calculation
  • Time complexity: O(n²) for an n×n matrix

Conclusion

Matrix row and column sum calculation in C uses nested loops with proper indexing. The key is maintaining correct loop order and resetting sum variables for each iteration.

Updated on: 2026-03-15T13:07:19+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements