Skip to content

Instantly share code, notes, and snippets.

@landjd19
Created September 18, 2018 01:06
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 landjd19/28df3c61c867946273c5aa8e4425877d to your computer and use it in GitHub Desktop.
Save landjd19/28df3c61c867946273c5aa8e4425877d to your computer and use it in GitHub Desktop.
IsPrime
/**
* Checks if input is a prime number.
*
* Jake Landaiche
* 1.0.0
*/
import java.util.*;
public class isPrime
{
Scanner sc = new Scanner(System.in);
public isPrime()
{
System.out.println("Enter an Integer: ");
int x = 1;
// initialise instance variables
while(x<100){
boolean y = isAPrime(x);
if(y == true)
System.out.println(x);
x++;
}
}
public boolean isAPrime(int y)
{
int counter = 2;
while(counter<=y){
if(y==2 || y==1){
//System.out.println("This is a prime number!");
return true;
}else if(y%counter == 0 && y != 2){
//counter = 2;
//System.out.println("This is not a prime number");
return false;
} else {
counter++;
if(counter == y){
//System.out.println("This is a prime number!");
return true;
}
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment