written 7.9 years ago by
teamques10
★ 64k
|
•
modified 7.5 years ago
|
import java.io.*;
class tabular {
public static void main(String args[]) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of rows");
int row = Integer.parseInt(br.readLine());
System.out.println("Enter the number of columns");
int col = Integer.parseInt(br.readLine());
int A[] [] = new int[10][10];
System.out.println("Enter the numbers below: ");
for(int r=0; r<row; r++) {
for(int c=0; c<col; c++) {
System.out.print("INPUT ["+ r +"] ["+ c +"] = ");
A[r][c] = Integer.parseInt(br.readLine());
}
}
System.out.println("\nThe matrix generated is: ");
//Calculating the sum of rows
int r;
int c;
for(r=0; r<row; r++) {
int tempRow = 0;
for(c=0; c<col; c++) {
tempRow += A[r][c];
}
A[r][c] = tempRow;
}
//Calculating sum of the column
for(c=0; c<=col; c++) {
int tempCol= 0;
for(r=0; r<row; r++) {
tempCol += A[r][c];
}
A[r][c] = tempCol;
}
//Display the tabular layout
for(r=0; r<=row; r++) {
for(c=0; c<=col; c++) {
System.out.print( A[r][c] + " ");
}
System.out.println();
}
}
}
OUTPUT
--------
Enter the number of rows
2
Enter the number of columns
3
Enter the numbers below:
INPUT [0] [0] = 1
INPUT [0] [1] = 2
INPUT [0] [2] = 3
INPUT [1] [0] = 4
INPUT [1] [1] = 5
INPUT [1] [2] = 6
The matrix generated is:
1 2 3 6
4 5 6 15
5 7 9 21
Edit: Thanks @Krish for finding the bug. It's fixed and this program works fine now.