Advertisements
Advertisements
Question
Write a program to accept a double dimensional array of size 5 × 6 and find the sum of the elements of each column.
Code Writing
Advertisements
Solution
import java.util.*;
public class DDA5
{
public void main()
{
Scanner sc = new Scanner(System.in);
int AR[][] = new int[5][6];
int r, c;
System.out.println("Filling array: ");
for(r = 0; r < 5; r++)
{
for(c = 0; c < 6; c++)
{
//System.out.print("Cell[" + r+"]["+c +"] : " );
AR[r][c] = (int)(Math.random() * 100);
}
}
System.out.println("The array is : ");
for(r = 0; r < 5; r++)
{
for(c = 0; c < 6; c++)
{
System.out.print(AR[r][c] + "\t");
}
System.out.println();
}
System.out.println("The Sum of each Column is : ");
for(c = 0; c < 6; c++)
{
int sum = 0;
for(r = 0; r < 5; r++)
{
sum = sum + AR[r][c];
}
System.out.println("Sum of Elements of Column num " + c + " is : " + sum);
}
}
}
Output:
Filling array:
The array is:
63 62 33 57 79 26
70 54 84 42 11 82
83 74 16 67 31 82
70 9 92 99 13 19
92 49 27 83 12 44
The Sum of each Column is :
Sum of Elements of Column num 0 is : 378
Sum of Elements of Column num 1 is : 248
Sum of Elements of Column num 2 is : 252
Sum of Elements of Column num 3 is : 348
Sum of Elements of Column num 4 is : 146
Sum of Elements of Column num 5 is : 253
shaalaa.com
Is there an error in this question or solution?
