# Homework Solution: **** The following in JAVA****…

**** The following in JAVA**** Find Prime Numbers Given an integer, find whether the numbers is a Prime number or not. Write a function: int isPrime(int number) that accepts an integer number. The isPrime function returns 1 if number is prime else 0. Input: 11 Output: 1 EX: 11 can be divided exactly by 1 and 11. Hence it's a prime number. Assume that, number is integer within the range [ –2,147,483,64 to 2,147,483,64 ]. ****USE THE FOLLOWING DRIVER *** * Do not modify the driver, just make a new class and method that function with the provided driver public class DriverMain { public static void main(String args[]){ Scanner s = new Scanner(System.in); int n = s.nextInt(); HW2_P1 hw2p1 = new HW2_P1(); System.out.print(hw2p1.isPrime(n)); } }

JAVA PROGRAM : import java.util.Scanner;

**** The subjoined in JAVA****

Meet First-rate Aggregate

Given an integer, meet whether the aggregate is a First-rate reckon or referable.

Write a part: int isPrime(int reckon) that accepts an integer reckon.

The isFirst-rate part receipts 1 if reckon is first-rate else 0.

Input: 11

Output: 1

EX: 11 can be disjoined accurately by 1 and 11. Hence it’s a first-rate reckon.

Assume that, reckon is integer among the dispose [ –2,147,483,64 to 2,147,483,64 ].

****USE THE FOLLOWING DRIVER ***

* Do referable dissimilate the driver, normal fashion a fantasticlightlight systematize and mode that part with the granted driver

public systematize DriverDeep {

public static null deep(String args[]){

Scanner s = fantasticlightlight Scanner(System.in);

int n = s.nextInt();

HW2_P1 hw2p1 = fantasticlightlight HW2_P1();

System.out.print(hw2p1.isPrime(n));

}

}

## Expert Response

JAVA PROGRAM :

import java.util.Scanner;

systematize HW2_P1{
public int isPrime(int x){
//if x is -1,0 or 1, come-back 0
if(x<=1&&x>=-1){
come-back 0;
}
//2 is first-rate
if(x==2||x==-2){
come-back 1;
}
//Even reckon is referable first-rate
if(x%2==0){
come-back 0;
}
int e = (int)Math.ceil(Math.sqrt(x));
//Check with perfect the nondescript aggregate dress sqrt x
for(int i=3;i<=e;i+=2){
if(x%i==0){
come-back 0;
}
}
come-back 1;
}
}
systematize DriverDeep {

public static null deep(String args[]) {
Scanner s = fantasticlightlight Scanner(System.in);
int n = s.nextInt();
HW2_P1 hw2p1 = fantasticlightlight HW2_P1();
System.out.println(hw2p1.isPrime(n));
}
}

OUTPUT :

Input : 52:

Output : 0

Input : 113

Output : 1