0% found this document useful (0 votes)
2 views3 pages

Program 1

The document outlines a Java program that accepts integers in an m x n matrix and calculates the sum of each row and column. It provides a sample input and output along with a detailed algorithm for implementation. Additionally, it includes a variable description table for clarity on the data types and purposes of the variables used in the program.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Program 1

The document outlines a Java program that accepts integers in an m x n matrix and calculates the sum of each row and column. It provides a sample input and output along with a detailed algorithm for implementation. Additionally, it includes a variable description table for clarity on the data types and purposes of the variables used in the program.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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.

You might also like