Created
September 17, 2018 15:13
-
-
Save feehe21/1489281138a1bbce4c69781fcbf9f26a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class isAPrime | |
{ | |
Scanner scan = new Scanner(System.in); | |
public isAPrime(){ | |
System.out.println("Enter a number to test if its prime"); | |
class variables | |
int y = scan.nextInt(); | |
int count = 2; | |
int test; | |
//int max = 100; | |
//int min = 2; | |
//int y = (int)(Math.random() * ((max - min) + 1)) + min;//inclusive | |
//System.out.println("Number = " + y); | |
boolean prime = isPrime(y); | |
System.out.println(prime); | |
System.out.println("All primes under 100:"); | |
while(count <= 100){//test every number under 100 for primeness | |
test = allPrime(count); | |
if(test == 1){ | |
System.out.print(count + " "); | |
} | |
count ++; | |
} | |
} | |
public boolean isPrime(int y) | |
{//test user inputted number | |
int compare = 2; | |
int remainder = 0; | |
boolean prime = false; | |
while(compare < y){ | |
remainder = y%compare; | |
if(remainder == 0){ | |
compare = y; | |
}else if (compare == (y-1)){ | |
prime = true; | |
} | |
compare ++; | |
} | |
return prime; | |
} | |
public int allPrime(int x){//test all numbers | |
int compare = 2; | |
int remainder = 0; | |
int yes = 0; | |
while(compare < x){ | |
remainder = x%compare; | |
if(remainder == 0){ | |
compare = x; | |
}else if (compare == (x-1)){ | |
yes = 1; | |
} | |
compare ++; | |
} | |
return yes; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment