Skip to content

Instantly share code, notes, and snippets.

@feehe21
Created September 17, 2018 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feehe21/1489281138a1bbce4c69781fcbf9f26a to your computer and use it in GitHub Desktop.
Save feehe21/1489281138a1bbce4c69781fcbf9f26a to your computer and use it in GitHub Desktop.
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