import [Link].
Random;
public class Surname_2Dim {
public static void main(String[] args) {
//2D array of 5*5 dimension
int[][] a=new int[5][5];
//2D array element between 10-75
initArray(a, 10, 75);
//Step 1: Output the array Elements
[Link]("1) Output the array Elements");
printElements(a);
[Link]();
//Step 2: Output the sum of prime numbers in array
[Link]("2) Output the sum of prime numbers in array");
sumPrime(a);
[Link]();
//Step 3: Output the elements in main diagonal
[Link]("3) Output the elements in main diagonal");
mainDiagonal(a);
[Link]();
//Step 4: Output the sum of the elements below diagonal
[Link]("4) Output the sum of the elements below diagonal");
sumBelowDiagonal(a);
[Link]();
//Step 5: Output the sum of the elements above diagonal
[Link]("5) Output the sum of the elements above diagonal");
sumAboveDiagonal(a);
[Link]();
//Step 6: Output the odd elements below diagonal
[Link]("6) Output the odd elements below diagonal");
oddBelowDiagonal(a);
[Link]();
//Step 7: Output the even elements above diagonal
[Link]("7) Output the even elements above diagonal");
evenAboveDiagonal(a);
[Link]();
}
//Initialize array a with random numbers between range min to max
static void initArray(int[][] a,int min,int max) {
Random rand = new Random();
for(int i=0;i<[Link];i++) {
for(int j=0;j<a[i].length;j++)
a[i][j]=min + ([Link](Integer.MAX_VALUE) % ((max-min)+1));
}
}
//Print all the array elements in formatted manner
static void printElements(int[][] a) {
for(int i=0;i<[Link];i++) {
for(int j=0;j<a[i].length;j++)
[Link]("%2d\t", a[i][j]);
[Link]();
}
}
//Check if num is prime or not if it is return true otherwise, it is false
static boolean isPrime(int num) {
double sqroot = [Link](num*1.0);
for(int i=2;i<=sqroot;i++) {
if(num%i==0)
return false;
}
return true;
}
//Sum up all the prime numbers in the array a
static void sumPrime(int[][] a) {
int sum=0;
for(int i=0;i<[Link];i++) {
for(int j=0;j<a[i].length;j++)
if(isPrime(a[i][j]))
sum+=a[i][j];
}
[Link](sum);
}
//Print the main diagonal elements
static void mainDiagonal(int[][] a) {
for(int i=0;i<[Link];i++)
[Link]("%2d\t",a[i][i]);
[Link]();
}
//Calculate and print the sum of elements below diagonal
static void sumBelowDiagonal(int[][] a) {
int sum=0;
for(int i=0;i<[Link];i++) {
for(int j=0;j<a[i].length;j++)
if(i>j) {
sum+=a[i][j];
}
}
[Link](sum);
}
//Calculate and print the sum of elements above diagonal
static void sumAboveDiagonal(int[][] a) {
int sum=0;
for(int i=0;i<[Link];i++) {
for(int j=0;j<a[i].length;j++)
if(i<j) {
sum+=a[i][j];
}
}
[Link](sum);
}
//Print the odd elements below diagonal
static void oddBelowDiagonal(int[][] a) {
for(int i=0;i<[Link];i++) {
for(int j=0;j<a[i].length;j++)
if(i>j && a[i][j]%2==1) {
[Link]("%2d\t",a[i][j]);
}
}
[Link]();
}
//Print the even elements above diagonal
static void evenAboveDiagonal(int[][] a) {
for(int i=0;i<[Link];i++) {
for(int j=0;j<a[i].length;j++)
if(i<j && a[i][j]%2==0) {
[Link]("%2d\t",a[i][j]);
}
}
[Link]();
}
}