PROGRAM-1
Problem statement:
Write a program in java to accept integers in a m x n matrix. Then find
the sum of each row elements as well as column elements.
Sample Input:
m=3
n=3
1 2 3
4 5 6
7 8 9
Sample Output:
Input array
1 2 3
4 5 6
7 8 9
Row 0: Sum=6
Row 1: Sum=15
Row 2: Sum=24
Column 0: Sum=12
Column 1: Sum=15
Column 2: Sum=18
Algorithm:
Step 1:Start.
Step 2:Create a scanner object for reading input.
Step 3:Declare variables:
Step 3A- rand c for the number of rows and column.
Step 3B- i and j for loop index.
Step 3C- rs for row sum and cs for column sum.
Step 3D- arr[][] for store the 2D array.
Step 4:Input array size.
Step 5:Declare the array of size r x c.
Step 6:Input array elements:
Step 6A- Loop through each row i, from 0 to r-1.
Step 6Ai= Loop through each column j, from 0 to c-1.
Step 6Aia→ Input array elements for arr[i][j].
Step 7:Calculate and display row sum:
Step 7A- Loop through each row i, from 0 to r-1.
Step 7Ai= Loop through each column j, from 0 to c-1.
Step 7Aia→ Add arr[i][j] in rs.
Step 7Aii= print sum of the rows.
Step 7Aiii= Set rs=0.
Step 8:Calculate and display column sum:
Step 8A- Loop through each column j, from 0 to c-1.
Step 8Ai= Loop through each row i, from 0 to r-1.
Step 8Aia→ Add arr[i][j] in cs.
Step 8Aii= print sum of the colums.
Step 8Aiii= Set cs=0.
Step 9:End.
Variable Description Table
Variable name Data type Purpose
i,j int Loop counter.
m,n int Row and column size.
a[][] Int To store array data.
rowSum,colSum int To store the sum of
row and column.