Guest User

Untitled

a guest
Jan 11th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. public class NthPrime{
  2.     public static void main(String[] args){
  3.        
  4.     System.out.println("Enter the nth prime number you wish to find:");
  5.     int x = IO.readInt();
  6.        
  7.         int nth;
  8.        
  9.         if (x <= 0) {
  10.             IO.reportBadInput();
  11.             return;
  12.         }
  13.        
  14.     boolean isPrime = true;
  15.        
  16.     int count = 0; //inital count
  17.     int n = 2; //starting prime number if x = 1
  18.        
  19.         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.
  20.        
  21.             for(int i = 2; i < n; i++){ //determines if the number is prime
  22.                
  23.                 if (n % i ==0){
  24.             isPrime = false;
  25.             break;
  26.         }
  27.                
  28.                 isPrime = true;
  29.                
  30.             } //ends if isPrime is true
  31.            
  32.         if (isPrime == true) { //if n is prime count get added 1
  33.         count = count + 1;
  34.         }
  35.            
  36.     } //end determining when to stop for prime number
  37.        
  38.         nth = n - 1;
  39.        
  40.         IO.outputIntAnswer(nth);
  41.     return;
  42.     }
  43. }
Add Comment
Please, Sign In to add comment