Explain how this code fragment checks the prime numbers in the array. 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; }
Explain how this code fragment checks the prime numbers in the array. 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; }
Chapter9: Advanced Array Concepts
Section: Chapter Questions
Problem 19RQ
Related questions
Question
Explain how this code fragment checks the prime numbers in the array.
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;
}
![71
E Quisora_2Dim.java
public class Quisora_2Dim {
public static void main(String[] args) { //main method
int[][] a=new int[5][5]; //create a 2D array of 5*5 dimension
initArray(a, 10, 50); //initialize all the elements in 2D array element between 10-50
//Solution 1 Output the array Elements
System.out.printin("Output the array Elements");
printElements(a);
System.out.println();
//Solution 2 Output the sum of prime numbers in array
System.out.printin("Output the sum of prime numbers in array");
sumPrime (a);
System.out.println();
//Solution 3 Output the elements in main diagonal
System.out.printin("Output the elements in main diagonal");
mainDiagonal(a);
System.out.println();
//Solution 4 Output the sum of the elements below diagonal
System.out.printin("Output the sum of the elements below diagonal");
sumBelowDiagonal(a);
System.out.println();
//Solution 5 Output the sum of the elements above diagonal
System.out.printin("Output the sum of the elements above diagonal");
sumAboveDiagonal(a);
System.out.printin();
//Solution 6 Output the odd elements below diagonal
System.out.printin("Output the odd elements below diagonal");
oddBelowDiagonal(a);
System.out.println();
//Solution 7 Output the even
System.out.printin("Output the even elements above diagonal");
evenAboveDiagonal(a);
System.out.println();
above diagonal
}
//method initialize the array a with random numbers between range min to max
static void initArray(int[][] a,int min,int max) {
//to generate random num
Random rand = new Random();
//traverse each element in the array and store a random number
for (int i=0;i<a.length;i++) {
for (int j=0;j<a[i].length;j++)
//generate a random num in range min to max
a[i][j]=min + (rand.nextInt(Integer.MAX_VALUE) % ((max-min)+1));
//method prints 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();
}
}
//method checks if num is prime or not if it is return true otherwise false
static boolean isPrime(int num) {
double sqroot = Math.sqrt(num*1.0);
//check each number from 2 to sqrt(num) if it divides num then it is not a prime number
for (int i=2;i<=sqroot;i++) {
if(num%i==0)
return false;
}
//we survived all the divisor that means num is prime
return true;
}
//method 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 this element is prime then add it to sum
if(isPrime(a[i][j]))
sum+=a[i][j];
}
System.out.println(sum);
//method prints the main diagonal elements s
static void mainDiagonal(int[][] a) {
for (int i=0;i<a.length;i++)
System.out.printf("%2d\t",a[i][i]);
System.out.println();
}
//method 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 element belongs to below diagonal then add it to sum
if(i>j) {
sum+=a[i][j];
System.out.println(sum);
}
static void sumAboveDiagonal(int[][] a) {
int sum=0;
//method calculate and print the sum of elements above diagonal
for (int i=0;i<a.length;i++) {
for (int j=0;j<a[i].length;j++)
//if element belongs to above diagonal then add it to sum
if(i<j) {
sum+=a[i][j];
}
}
System.out.println(sum);
}
//method prints 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++)
1/if element belongs to below diagonal and it is odd then print it
if(i>j && a[i][j]%2==1) {
System.out.printf("%2d\t",a[i][j]);
}
}
System.out.println();
}](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fd47f2570-65c7-46a3-9b27-fc057c29a6b8%2Fc23d30fd-b9fe-458b-ab1c-ac527d5b1ed9%2Frc5936_processed.jpeg&w=3840&q=75)
Transcribed Image Text:71
E Quisora_2Dim.java
public class Quisora_2Dim {
public static void main(String[] args) { //main method
int[][] a=new int[5][5]; //create a 2D array of 5*5 dimension
initArray(a, 10, 50); //initialize all the elements in 2D array element between 10-50
//Solution 1 Output the array Elements
System.out.printin("Output the array Elements");
printElements(a);
System.out.println();
//Solution 2 Output the sum of prime numbers in array
System.out.printin("Output the sum of prime numbers in array");
sumPrime (a);
System.out.println();
//Solution 3 Output the elements in main diagonal
System.out.printin("Output the elements in main diagonal");
mainDiagonal(a);
System.out.println();
//Solution 4 Output the sum of the elements below diagonal
System.out.printin("Output the sum of the elements below diagonal");
sumBelowDiagonal(a);
System.out.println();
//Solution 5 Output the sum of the elements above diagonal
System.out.printin("Output the sum of the elements above diagonal");
sumAboveDiagonal(a);
System.out.printin();
//Solution 6 Output the odd elements below diagonal
System.out.printin("Output the odd elements below diagonal");
oddBelowDiagonal(a);
System.out.println();
//Solution 7 Output the even
System.out.printin("Output the even elements above diagonal");
evenAboveDiagonal(a);
System.out.println();
above diagonal
}
//method initialize the array a with random numbers between range min to max
static void initArray(int[][] a,int min,int max) {
//to generate random num
Random rand = new Random();
//traverse each element in the array and store a random number
for (int i=0;i<a.length;i++) {
for (int j=0;j<a[i].length;j++)
//generate a random num in range min to max
a[i][j]=min + (rand.nextInt(Integer.MAX_VALUE) % ((max-min)+1));
//method prints 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();
}
}
//method checks if num is prime or not if it is return true otherwise false
static boolean isPrime(int num) {
double sqroot = Math.sqrt(num*1.0);
//check each number from 2 to sqrt(num) if it divides num then it is not a prime number
for (int i=2;i<=sqroot;i++) {
if(num%i==0)
return false;
}
//we survived all the divisor that means num is prime
return true;
}
//method 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 this element is prime then add it to sum
if(isPrime(a[i][j]))
sum+=a[i][j];
}
System.out.println(sum);
//method prints the main diagonal elements s
static void mainDiagonal(int[][] a) {
for (int i=0;i<a.length;i++)
System.out.printf("%2d\t",a[i][i]);
System.out.println();
}
//method 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 element belongs to below diagonal then add it to sum
if(i>j) {
sum+=a[i][j];
System.out.println(sum);
}
static void sumAboveDiagonal(int[][] a) {
int sum=0;
//method calculate and print the sum of elements above diagonal
for (int i=0;i<a.length;i++) {
for (int j=0;j<a[i].length;j++)
//if element belongs to above diagonal then add it to sum
if(i<j) {
sum+=a[i][j];
}
}
System.out.println(sum);
}
//method prints 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++)
1/if element belongs to below diagonal and it is odd then print it
if(i>j && a[i][j]%2==1) {
System.out.printf("%2d\t",a[i][j]);
}
}
System.out.println();
}
![» 71
E Quisora_2Dim.java
JLU L IC
for(int i=0;i<a.length;i++) {
for (int j=0;j<a[i].length;j++)
//if element belongs to below diagonal and it is odd then print it
if(i>j && a[i][j]%2==1) {
System.out.printf("%2d\t", a[i][j]);
}
}
System.out.println();
}
//method prints 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 element belongs to above diagonal and it is even then print it
if(i<j && a[i][j]%2==0) {
System.out.printf("%2d\t", a[i][j]);
}
}
System.out.println();
}
}](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fd47f2570-65c7-46a3-9b27-fc057c29a6b8%2Fc23d30fd-b9fe-458b-ab1c-ac527d5b1ed9%2F9xb0e8_processed.jpeg&w=3840&q=75)
Transcribed Image Text:» 71
E Quisora_2Dim.java
JLU L IC
for(int i=0;i<a.length;i++) {
for (int j=0;j<a[i].length;j++)
//if element belongs to below diagonal and it is odd then print it
if(i>j && a[i][j]%2==1) {
System.out.printf("%2d\t", a[i][j]);
}
}
System.out.println();
}
//method prints 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 element belongs to above diagonal and it is even then print it
if(i<j && a[i][j]%2==0) {
System.out.printf("%2d\t", a[i][j]);
}
}
System.out.println();
}
}
Expert Solution
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
![New Perspectives on HTML5, CSS3, and JavaScript](https://www.bartleby.com/isbn_cover_images/9781305503922/9781305503922_smallCoverImage.gif)
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:
9781305503922
Author:
Patrick M. Carey
Publisher:
Cengage Learning
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
![New Perspectives on HTML5, CSS3, and JavaScript](https://www.bartleby.com/isbn_cover_images/9781305503922/9781305503922_smallCoverImage.gif)
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:
9781305503922
Author:
Patrick M. Carey
Publisher:
Cengage Learning