Created
September 18, 2018 01:06
-
-
Save landjd19/28df3c61c867946273c5aa8e4425877d to your computer and use it in GitHub Desktop.
IsPrime
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
/** | |
* 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