Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class NthPrime{
- public static void main(String[] args){
- System.out.println("Enter the nth prime number you wish to find:");
- int x = IO.readInt();
- int nth;
- if (x <= 0) {
- IO.reportBadInput();
- return;
- }
- boolean isPrime = true;
- int count = 0; //inital count
- int n = 2; //starting prime number if x = 1
- for(n = 2; count < x; n++) { //when count is less than x, it finds if the number is prime then count + 1, if not 1 is added to n until prime is found.
- for(int i = 2; i < n; i++){ //determines if the number is prime
- if (n % i ==0){
- isPrime = false;
- break;
- }
- isPrime = true;
- } //ends if isPrime is true
- if (isPrime == true) { //if n is prime count get added 1
- count = count + 1;
- }
- } //end determining when to stop for prime number
- nth = n - 1;
- IO.outputIntAnswer(nth);
- return;
- }
- }
Add Comment
Please, Sign In to add comment