Write a C program to perform 3X3 matrix operations

In C, we can perform various operations on a 3x3 matrix such as calculating row sums, column sums, and diagonal sums. This program demonstrates how to read a 3x3 matrix and compute these basic operations.

Syntax

int matrix[3][3];  // 3x3 matrix declaration
// Row sum: matrix[i][0] + matrix[i][1] + matrix[i][2]
// Column sum: matrix[0][j] + matrix[1][j] + matrix[2][j]
// Main diagonal: matrix[0][0] + matrix[1][1] + matrix[2][2]
// Anti-diagonal: matrix[0][2] + matrix[1][1] + matrix[2][0]

Algorithm

Step 1: Declare a 3x3 matrix
Step 2: Read 9 numbers from user
Step 3: Store numbers in matrix form
        // matrix[0][0] matrix[0][1] matrix[0][2]
        // matrix[1][0] matrix[1][1] matrix[1][2]
        // matrix[2][0] matrix[2][1] matrix[2][2]
Step 4: Calculate row sums for each row
Step 5: Calculate column sums for each column
Step 6: Calculate diagonal sums (main and anti-diagonal)
Step 7: Display all results

Example

Here's a complete program that performs 3x3 matrix operations −

#include <stdio.h>

int main() {
    int matrix[3][3];
    int i, j;
    
    // Reading matrix elements
    printf("Enter 9 numbers for 3x3 matrix:<br>");
    for(i = 0; i < 3; i++) {
        for(j = 0; j < 3; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
    
    // Displaying the matrix
    printf("\nMatrix:<br>");
    for(i = 0; i < 3; i++) {
        for(j = 0; j < 3; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("<br>");
    }
    
    // Row sums
    printf("\nRow totals: ");
    for(i = 0; i < 3; i++) {
        int rowSum = 0;
        for(j = 0; j < 3; j++) {
            rowSum += matrix[i][j];
        }
        printf("%d ", rowSum);
    }
    printf("<br>");
    
    // Column sums
    printf("Column totals: ");
    for(j = 0; j < 3; j++) {
        int colSum = 0;
        for(i = 0; i < 3; i++) {
            colSum += matrix[i][j];
        }
        printf("%d ", colSum);
    }
    printf("<br>");
    
    // Diagonal sums
    int mainDiagonal = matrix[0][0] + matrix[1][1] + matrix[2][2];
    int antiDiagonal = matrix[0][2] + matrix[1][1] + matrix[2][0];
    
    printf("Diagonal totals: %d %d<br>", mainDiagonal, antiDiagonal);
    
    return 0;
}
Enter 9 numbers for 3x3 matrix:
2 4 6 3 5 7 8 9 2

Matrix:
2 4 6 
3 5 7 
8 9 2 

Row totals: 12 15 19 
Column totals: 13 18 15 
Diagonal totals: 9 19

Key Points

  • Use a 2D array int matrix[3][3] for better code organization and readability.
  • Main diagonal elements are at positions where row index equals column index (i == j).
  • Anti-diagonal elements are at positions where row + column index equals 2.
  • Nested loops make matrix operations more scalable and maintainable.

Conclusion

This program demonstrates basic 3x3 matrix operations in C using 2D arrays. The structured approach with loops makes the code more readable and easier to extend for larger matrices.

Updated on: 2026-03-15T13:40:37+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements