Skip to main content

17. Column-wise sum of a matrix


/* Column-wise sum of a matrix. */


#include <stdio.h>
int main()
{
    int n, m, i, j, sum;
    printf("Enter the number of rows and columns of matrix: ");
    scanf("%d %d", &n, &m);
    int a[n][m];
    for(i = 0; i < n; i++){
        for(j = 0; j < m; j++){
            printf("Enter element (%d%d) of matric A: ", i+1, j+1);
            scanf("%d", &a[i][j]);
        }
    }
    printf("The matrix you entered is.....\n");
    for(i = 0; i < n; i++){
        for(j = 0; j < m; j++)
        printf("%d\t"a[i][j]);
        printf("\n");
    }
    for(i = 0; i < m; i++){
        sum = 0;
        for(j = 0; j < n; j++){
            sum += a[j][i];
        }
        printf("Sum of column %d elements = %d\n", i+1, sum);
    }
    return 0;
}

Comments