Instruction: Create a java program that generates elements (randomly from 10 - 75) of a 2-dimensional array (5x5) using the Random Class then perform the following: 1) Output the array elements 2) Output the sum of prime numbers in the array 3) Output the elements in the main diagonal. 4) Output the sum of the elements below the diagonal. 5) Output the sum of the elements above the diagonal. 6) Output the odd numbers below the diagonal. 7) Output the even numbers above the diagonal. Sample Input/Output: Depicted below are sample outputs when the program is executed (the items in bold characters are input from the user, while the items in bold italic are generated, calculated and printed by the program):
use two dimensional array and put comment on what each line of code does
Step by step
Solved in 2 steps with 2 images
please put comments on these line of codes of what these do:
private static void evenNumbersAboveDiagonal(int[][] arr) {
for(int j=1;j<5;j++){
for(int i=j-1;i>=0;i--){
if(arr[i][j]%2 == 0)
System.out.print(arr[i][j] + " ");
}
}
System.out.println();
}
private static void oddNumbersBelowDiagonal(int[][] arr) {
for(int i=0;i<5;i++){
for(int j=0;i>j;j++){
if(arr[i][j]%2==1)
System.out.print(arr[i][j] + " ");
}
}
System.out.println();
}
private static int sumOfElementsAboveDiagonal(int[][] arr) {
int sum = 0;
for (int j=1;j<5;j++) {
for (int i=j-1;i>=0;i--) {
sum += arr[i][j];
}
}
return sum;
}
private static int sumOfElementsBelowDiagonal(int[][] arr) {
int sum = 0;
for(int i=0;i<5;i++){
for(int j=0;i>j;j++) {
sum += arr[i][j];
}
}
return sum;
}
private static void elementsOfMainDiagonal(int[][] arr) {
for (int i=0;i<5;i++) {
for (int j=0; j<5;j++) {
if(i == j)
System.out.print(arr[i][j] + " ");
}
}
System.out.println();
}
private static int sumOfPrime(int[][] arr) {
int sum = 0;
for(int i=0;i<5;i++){
for(int j=0;j<5;j++) {
if(isPrime(arr[i][j]))
sum+=arr[i][j];
}
}
return sum;
}
private static boolean isPrime(int n){
if(n<=1)
return false;
for(int i=2;i<n;i++)
if(n%i == 0)
return false;
return true;
}
}