Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a prime number checker. A very simple algorithm for determining whether a

ID: 672872 • Letter: W

Question

Write a prime number checker. A very simple algorithm for determining whether a number, n, is prime would be:

For each number i between 2 and the square root of n: see if the remainder of n/i is equal to 0. If it is, return that this value is not a prime number. If none of the i's evenly divide into n, return that it is a prime number.

Your job is to write a class called Prime that has a static method which checks whether an integer is prime or not using the algorithm listed above. This method should take in an int and return a boolean. Write a main method that tests your prime number checker on several different inputs (these can be hardcoded).

(In Java)

Explanation / Answer

import java.util.Scanner;
class PrimeCheck
{
public static void main(String args[])
{      
int temp;
boolean isPrime=true;
Scanner scan= new Scanner(System.in);
System.out.println("Enter a number for check:");
int num=scan.nextInt();
for(int i=2;i<=num/2;i++)
{
temp=num%i;
if(temp==0)
{
isPrime=false;
break;
}
}
if(isPrime)
System.out.println(num + " is Prime Number");
else
System.out.println(num + " is not Prime Number");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote