forked from sPredictorX1708/Ultimate-Java-Resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIter.java
More file actions
40 lines (34 loc) · 1.22 KB
/
Iter.java
File metadata and controls
40 lines (34 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Java program to the nth prime number using Iteration
import java.util.Scanner;
public class NthPrime {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//Taking input
int nth = sc.nextInt();
int num, count, i;
num=1;
count=0;
//This loop continues until the value
// of the count is less than n.
// If the condition is true then
// it will increase the value of num by 1.
while (count < nth){
num=num+1;
for (i = 2; i <= num; i++){
if (num % i == 0) {
// if the number has a divisor other
// than 1 or itself, we break out of the loop
break;
}
}
//The loop breaks and checks whether i is equal to num.
// If it is so then the value of count is increased by 1
// and then again checks the condition of while loop.
//When the while loop terminates we get our final value in the variable num
if ( i == num){
count = count+1;
}
}
System.out.println("Value of nth prime: " + num);
}
}