public class PrimeAdvance {
public static void main(String[] args) {
// int valueToCheck = 20; // value to check
int nRange = 50;
boolean isPrime = false;
for (int i = 2; i <= nRange; i++) {
if (i >= 2) {
isPrime = true; // first check and assume it's prime number
// try divide valueToCheck
// with all number less than it self
// and begining from 2
for (int j = 2; j < i; j++) {
if (i % j == 0) {
//if divides exactly so stop the loop and it must be not prime
isPrime = false;
break; // no need to check again
}
}
}
if(isPrime){
System.out.println(i);}
}
//System.out.println("is " + valueToCheck + " Prime ? ");
//System.out.println("the answer is " + isPrime);
}
}
can someone show me how to create a tester class from this and a user input for any number you want to check for prime number?
Question
GreenInspiration
public class PrimeAdvance { public static void main(String[] args) { // int valueToCheck = 20; // value to check int nRange = 50; boolean isPrime = false; for (int i = 2; i <= nRange; i++) { if (i >= 2) { isPrime = true; // first check and assume it's prime number // try divide valueToCheck // with all number less than it self // and begining from 2 for (int j = 2; j < i; j++) { if (i % j == 0) { //if divides exactly so stop the loop and it must be not prime isPrime = false; break; // no need to check again } } } if(isPrime){ System.out.println(i);} } //System.out.println("is " + valueToCheck + " Prime ? "); //System.out.println("the answer is " + isPrime); } }can someone show me how to create a tester class from this and a user input for any number you want to check for prime number?
Link to comment
Share on other sites
0 answers to this question
Recommended Posts