0% found this document useful (0 votes)
41 views

2d Array Row Sum and Column Sum

This Java code defines a 2D array and accepts user input for the number of rows and columns. It then prompts the user to input elements for each row, calculating a running sum. It repeats this process for each column, summing elements. Finally, it outputs the populated 2D array with each element and calculated row/column sums.

Uploaded by

abc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

2d Array Row Sum and Column Sum

This Java code defines a 2D array and accepts user input for the number of rows and columns. It then prompts the user to input elements for each row, calculating a running sum. It repeats this process for each column, summing elements. Finally, it outputs the populated 2D array with each element and calculated row/column sums.

Uploaded by

abc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

Scanner;
public class testcode{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(" enter the number of rows");
int r = sc.nextInt();
System.out.println(" enter the number of colums");
int c = sc.nextInt();

int ar[][] = new int[r+1][c+1];

for(int i = 0 ; i < ar.length-1 ; i ++){


int sum = 0;
for(int j = 0 ; j < ar[0].length ; j++){
if(j == ar[0].length-1){
ar[i][j] = sum;
break;
}else{
System.out.println("enter the element ");
ar[i][j] = sc.nextInt();
sum = sum+ar[i][j];
}
}
}

for(int i = 0 ; i < ar[0].length ; i++){


int sum = 0;
for(int j = 0 ; j <= ar.length-1 ; j++){
if(j == ar.length-1){
ar[j][i] = sum;
break;
}else{
sum = sum+ar[j][i];
}
}
}

for(int i = 0 ; i < ar.length ; i ++){


for(int j = 0 ; j <ar[0].length ; j++){
if(i == ar.length-1 && j == ar[0].length-1 ){
continue;
}
System.out.print( ar[i][j] + " ");
}
System.out.println(" ");
}

}
}

You might also like