Write a C program that create 3X3 matrix and replace last element of each row with sum of that row Elements.

SOLUTION .

 

#include <'stdio.h'>
#define SIZE 3
int main() {
    int matrix[SIZE][SIZE];
    int i, j;
    
    // Input matrix elements
    printf("Enter elements of a 3x3 matrix:\n");
    for (i = 0; i < SIZE; i++) {
        for (j = 0; j < SIZE - 1; j++) { // Take input for first two elements of each row
            scanf("%d", &matrix[i][j]);
        }
    }
    // Compute row sums and replace last element
    for (i = 0; i < SIZE; i++) {
        int sum = 0;
        for (j = 0; j < SIZE - 1; j++) {
            sum += matrix[i][j];
        }
        matrix[i][SIZE - 1] = sum; // Replace last element with row sum
    }
    
    // Display modified matrix
    printf("Modified 3x3 Matrix:\n");
    for (i = 0; i < SIZE; i++) {
        for (j = 0; j < SIZE; j++) {
            printf("%d\t", matrix[i][j]);
        }
        printf("\n");
    }
    
    return 0;
}
    

Leave a Reply

Your email address will not be published. Required fields are marked *

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.