0% found this document useful (0 votes)
29 views

2dim Java Code

The document defines methods to initialize and manipulate a 2D integer array in Java. It initializes a 5x5 array with random numbers, then prints: 1) all elements, 2) the sum of prime numbers, 3) main diagonal elements, 4) sum below diagonal, 5) sum above diagonal, 6) odd below diagonal elements, 7) even above diagonal elements. Methods are defined to initialize the array, print elements, check for primes, and calculate/print sums and elements based on array position.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

2dim Java Code

The document defines methods to initialize and manipulate a 2D integer array in Java. It initializes a 5x5 array with random numbers, then prints: 1) all elements, 2) the sum of prime numbers, 3) main diagonal elements, 4) sum below diagonal, 5) sum above diagonal, 6) odd below diagonal elements, 7) even above diagonal elements. Methods are defined to initialize the array, print elements, check for primes, and calculate/print sums and elements based on array position.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

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


System.out.println("1) Output the array Elements");
printElements(a);
System.out.println();

//Step 2: Output the sum of prime numbers in array


System.out.println("2) Output the sum of prime numbers in array");
sumPrime(a);
System.out.println();

//Step 3: Output the elements in main diagonal


System.out.println("3) Output the elements in main diagonal");
mainDiagonal(a);
System.out.println();

//Step 4: Output the sum of the elements below diagonal


System.out.println("4) Output the sum of the elements below diagonal");
sumBelowDiagonal(a);
System.out.println();

//Step 5: Output the sum of the elements above diagonal


System.out.println("5) Output the sum of the elements above diagonal");
sumAboveDiagonal(a);
System.out.println();

//Step 6: Output the odd elements below diagonal


System.out.println("6) Output the odd elements below diagonal");
oddBelowDiagonal(a);
System.out.println();

//Step 7: Output the even elements above diagonal


System.out.println("7) Output the even elements above diagonal");
evenAboveDiagonal(a);
System.out.println();
}
//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<a.length;i++) {
for(int j=0;j<a[i].length;j++)
a[i][j]=min + (rand.nextInt(Integer.MAX_VALUE) % ((max-min)+1));
}
}
//Print all the array elements in formatted manner
static void printElements(int[][] a) {
for(int i=0;i<a.length;i++) {
for(int j=0;j<a[i].length;j++)
System.out.printf("%2d\t", a[i][j]);
System.out.println();
}
}
//Check if num is prime or not if it is return true otherwise, it is false
static boolean isPrime(int num) {
double sqroot = Math.sqrt(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<a.length;i++) {
for(int j=0;j<a[i].length;j++)
if(isPrime(a[i][j]))
sum+=a[i][j];
}
System.out.println(sum);
}
//Print the main diagonal elements
static void mainDiagonal(int[][] a) {
for(int i=0;i<a.length;i++)
System.out.printf("%2d\t",a[i][i]);
System.out.println();
}
//Calculate and print the sum of elements below diagonal
static void sumBelowDiagonal(int[][] a) {
int sum=0;
for(int i=0;i<a.length;i++) {
for(int j=0;j<a[i].length;j++)
if(i>j) {
sum+=a[i][j];
}
}
System.out.println(sum);
}
//Calculate and print the sum of elements above diagonal
static void sumAboveDiagonal(int[][] a) {
int sum=0;
for(int i=0;i<a.length;i++) {
for(int j=0;j<a[i].length;j++)
if(i<j) {
sum+=a[i][j];
}
}
System.out.println(sum);
}
//Print the odd elements below diagonal
static void oddBelowDiagonal(int[][] a) {
for(int i=0;i<a.length;i++) {
for(int j=0;j<a[i].length;j++)
if(i>j && a[i][j]%2==1) {
System.out.printf("%2d\t",a[i][j]);
}
}
System.out.println();
}
//Print the even elements above diagonal
static void evenAboveDiagonal(int[][] a) {
for(int i=0;i<a.length;i++) {
for(int j=0;j<a[i].length;j++)
if(i<j && a[i][j]%2==0) {
System.out.printf("%2d\t",a[i][j]);
}
}
System.out.println();
}
}

You might also like