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;                 }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
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();
}
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();
}
}
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
steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Array
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.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education