Java

Write a program to store number in 4 * 4 matrix  in a two dimensional array. Find the sum of each row and sum of  the number each column of the matrix.

import java.util.*;

class MatrixRowColSum
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

int i;
int j;
int array[][] = new int[5][5];

for(i = 0; i < array.length-1; i++)
{
for(j = 0; j < array.length-1; j++)
{
System.out.print("Enter element " + " [" + i + "] " + " [" + j + "] : ");
array[i][j] = sc.nextInt();
}
}

for(i = 0; i < array.length-1; i++)
{
for(j = 0; j < array.length-1; j++)
{
array[i][4] += array[i][j];
array[4][i] += array[j][i]; 
}
}

for(i = 0; i < array.length; i++)
{
for(j = 0; j < array.length; j++)
{
System.out.print(array[i][j] + "\t");
}
System.out.println();
}
}
}


Output:



Post a Comment

0 Comments