Advertisements
Advertisements
Question
A double dimensional array is defined as N[4][4] to store numbers. Write a program to find the sum of all even numbers and product of all odd numbers of the elements stored in Double Dimensional Array (DDA).
12 | 10 | 15 | 17 |
30 | 11 | 32 | 71 |
17 | 14 | 29 | 31 |
41 | 33 | 40 | 51 |
Sample Output:
Sum of all even numbers:...........
Sum of all odd numbers:...........
Answer in Brief
Solution
import java.util.*;
class AQ15{
public static void main(String args[])
{Scanner ob = new Stanner(System.in);
int i, j, even = 0, odd = 1; // initialize odd = 1 to store product
int m[][] = new int[4][4];
System.out.println("Enter the numbers of the matrix");
for (i = 0; i < 4; i++)
{for (j = 0; j < 4; j++)
m[i][j] = ob.nextInt();
}
System.out.println("The numbers of the matrix are");
for (i = 0; i < 4; i++)
{for (j = 0; j < d; j++)
{ System.out.print(m[i][j] + " ");
if (m[i][j] % 2 == 0)
even += m[i][j]; // sum of all even numbers
else
odd *= m([i][j]; // product of all odd numbers
}
System.out.println();
}
System.out.println("Sum of even = " + even);
System.out.printIn("Product of odd = " + odd);
}}
shaalaa.com
Is there an error in this question or solution?
Chapter 3: Arrays (Single Dimensional and Double Dimensional) - EXERCISES [Page 242]