WHAT IS WRONG WITH THIS CODE?? Something with illegal start of expression
WHAT IS WRONG WITH THIS CODE?? Something with illegal start of expression
import java.util.Random;
public class FamilyName_2Dim {
public static void main(String[] args) {
//random class object
Random rand = new Random();
//declare and initiate variables
int i, j, k;
int min = 10;
int max = 75;
//define 2D array (5 by 5)
int array[][]=new int[5][5];
//iterate for loop
for(i=0;i<5;i++) {
for(j=0;j<5;j++) {
//generate random numbers from 10 to 75
array[i][j]=rand.nextInt(max - min + 1) + min;
}
}
//display text
System.out.println(" 1) Output the array elements");
//add space
System.out.println();
//iterate for loop
for(i=0;i<5;i++) {
for(j=0;j<5;j++) {
//display array
System.out.print("\t" + array[i][j] + "\t");
}
//add space
System.out.println();
}
//declare and initialize variable
int sumofprime = 0;
//iterate for loop
for(i=0;i<5;i++) {
for(j=0;j<5;j++) {
//declare and initialize variable
int factor = 0;
for(k=1;k<=array[i][j];k++) {
//count the factors of the element in the array
if(array[i][j]%k==0)
factor += 1;
}
//if the number of factors is 2 then the element is prime, so
if(factor == 2)
//find out the sum of such elements
sumofprime += array[i][j];
}
}
//display text
System.out.println("\n 2) Output the sum of prime numbers in the array \n\t" + sumofprime);
//display text
System.out.println("\n 3) Output the elements in the main diagonal");
//iterate for loop
for(i=0;i<5;i++) {
for(j=0;j<5;j++) {
//elements at indexes (0,0) (1,1) (2,2) etc are diagonals
if(i==j)
//display elements where i=j
System.out.print("\t" + array[i][j] + " ");
}
}
//declare and initialize variable
int sumbelowdiagonal = 0;
//iterate for loop
for(i=0;i<5;i++) {
//find out elements below the diagonal
for (j=i-1;j>=0;j--) {
//find out the sum
sumbelowdiagonal = sumbelowdiagonal + array[i][j];
}
}
//add space
System.out.println();
//display text
System.out.println("\n 4) Output the sum of elements below the diagonal \n\t" + sumbelowdiagonal);
//declare and initialize variable
int sumabovediagonal = 0;
//iterate for loop
for(i=0;i<5;i++) {
//find out elements below the diagonal
for (j=i-1;j>=0;j--) {
//find out the sum
sumabovediagonal= sumbelowdiagonal + array[j][i];
}
}
//display text
System.out.println("\n 5) Output the sum of elements above the diagonal \n\t" + sumabovediagonal);
//display text
System.out.println("\n 6) Output the odd numbers below the diagonal \n\t");
//iterate for loop
for(i=0;i<5;i++) {
//find out elements below the diagonal
for (j=i-1;j>=0;j--){
//check if the element is odd
if(array[i][j]%2!=0)
//display the elements
System.out.print("\t" + array[i][j] + " ");
}
}
//add space
System.out.println();
//display text
System.out.println("\n 7) Output the even numbers above the diagonal \n\t");
//iterate for loop
for(i=0;i<5;i++) {
//find out elements below the diagonal
for (j=i-1;j>=0;j--) {
//check if the element is even
if(array[j][i]%2==0) /
//display the elements
System.out.print("\t" + array[j][i] + " ");
}
}
}
}

Step by step
Solved in 3 steps with 2 images









