Find the sum of each row and column of a matrix with C.


#include <stdio.h>

int main()
{
    int A[3][3];
    int row, col, sum = 0;

    /* Input elements in matrix from user */
    printf("Enter elements in matrix of 3 %dx%d: \n", 3, 3);
    for (row = 0; row < 3; row++)
    {
        for (col = 0; col < 3; col++)
        {
            scanf("%d", &A[row][col]);
        }
    }

    /* Calculate sum of elements of each row of matrix */
    for (row = 0; row < 3; row++)
    {
        sum = 0;
        for (col = 0; col < 3; col++)
        {
            sum += A[row][col];
        }

        printf("Sum of elements of Row %d = %d\n", row + 1, sum);
    }

    /* Find sum of elements of each columns of matrix */
    for (row = 0; row < 3; row++)
    {
        sum = 0;
        for (col = 0; col < 3; col++)
        {
            sum += A[col][row];
        }

        printf("Sum of elements of Column %d = %d\n", row + 1, sum);
    }

    return 0;
}

Output

Enter elements in matrix of size 3x3 :
1 2 3
4 5 6
7 8 9
Sum of elements of Row 1 = 6
Sum of elements of Row 2 = 15
Sum of elements of Row 3 = 24
Sum of elements of Column 1 = 12
Sum of elements of Column 2 = 15
Sum of elements of Column 3 = 18