write a program in java to input element in a 3*4 matrix and display the sum of each column of the matrix
Answers
Answer:
Java program to input element in a 3*4 matrix and display the sum of each column of the matrix.
Explanation:
import java.io.*;
class GFG {
// Get the size m and n
static int m = 3;
static int n = 4;
static void column_sum(int arr[][])
{
// Function to calculate sum of each column }
/ int i,j,sum = 0;
System.out.print( "\n Sum of each column:\n\n");
// finding the column sum
for (i = 0; i < 3; ++i) {
for (j = 0; j < 4; ++j) {
// Add the element
sum = sum + arr[j][i];
}
// Print the column sum
System.out.println(
"Sum of the column "
+ i + " = " + sum);
// Reset the sum
sum = 0;
}
}
public static void main (String[] args) {
int i,j;
int [][]arr = new int[m][n];
// Get the matrix elements
int x = 1;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
arr[i][j] = x++;
// Get each column sum
column_sum(arr);
}
}