2d Array Row Sum and Column Sum

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 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