Question 7
Write a program in Java to accept 16 values (of int type) in a double dimension array
art[][] of order 4 x 4 and perform the following:
(i) calculate and print the sum of the elements of the first row.
(ü) calculate and print the sum of the elements of the last column.
(iii) print the array in matrix form.
For example if the values in the array entered are:
3 9 5 6
8 4 2 0
9 4 12 16
20 1 25 7
Then the output will be:
Sum of the first row: 23
Sum of last column : 29
Array in matrix form
3 9 5 6
8 4 2 0
9 4 12 16
20 1 25 7
Answers
Answer:
import java.util.*;
class DD
{
public static void main (String args[])
{
Scanner in= new Scanner (System.in);
int arr [][]=new int[4][4];
int sumrow=0,sumcolumn=0;
for( int i= 0;i<4;i++)
{
for( int j= 0;j<4;j++)
{
if(i==0)
{
sumrow=sumrow+arr[i][j];
}
if(j==3)
{
sumcolumn=sumcolumn+arr[i][j];
}
}
}
for( int i= 0;i<4;i++)
{
for( int j= 0;j<4;j++)
{
System.out.print(arr[i][j]);
}
System.out.println();
}
System.out.println(" Sum of first row" +
sumrow);
System.out.println("Sum of last column "
sumcolumn);
}
}
//hope it helped