2021-11-28

How can I print the sum of the columns as well?

package Matrix;

import java.util.Scanner;
public class Matrix
{
public static void main(String[] args) {
System.out.print("Enter 2D array size : ");
Scanner sc=new Scanner(System.in);
int rows=sc.nextInt();
int columns=sc.nextInt();

System.out.println("Enter array elements : ");    
         
int twoD[][]=new int[rows][columns];


for(int i=0; i<rows;i++)
{            
for(int j=0; j<columns;j++)
{
twoD[i][j]=sc.nextInt();
}
}
System.out.print("\n  \n");
for(int []x:twoD){
for(int y:x){
System.out.print(y+"        ");
}
System.out.println();
}
System.out.println("");
for(int i=0;i<columns;i++) {
int sum = 0;
for(int j=0;j<rows;j++)
{
sum=sum+twoD[i][j];
}
System.out.print(sum+" ");
sum=0;
System.out.print("       ");
}

}  

}

how can I print the sum of the columns as well? 
here is what I have so far
My expected output must be :
5     9      8   =   22
3     8      2   =   13
4     3      9   =   16
___________
12   20   19

how can I print the sum of the columns as well? here is what I have so far, And it seems like there is a problem with my code as well. Hope you can help me. I've been working on this for days and I'm new to java.

how can I print the sum of the columns as well? here is what I have so far, And it seems like there is a problem with my code as well. Hope you can help me. I've been working on this for days and I'm new to java.



from Recent Questions - Stack Overflow https://ift.tt/3nXotIv
https://ift.tt/eA8V8J

No comments:

Post a Comment