+ 6
How to create a java program to get 10001th prime number.
I have done many attempts but failed plzz anyone help me.
4 Réponses
+ 3
import java.util.Scanner;
public class Prime{
private int count = 0;
public Prime(int n){
int cur = 2;
boolean f = true;
while(n != count){
for(int i = 2;i*i<=cur;i++){
if(cur % i == 0){
f = false;
break;
}
f = true;
}
if(f){
System.out.println("#" + (count + 1) + ": " + cur);
count++;
}
cur++;
}
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter the amount of the prime numbers you want to display: ");
int num = in.nextInt();
new Prime(num);
}
}
The simplest answer to your question :-)
+ 8
check this java version i think you like!!
https://code.sololearn.com/c8KCZTNWXzC8/?ref=app
+ 4
@Qanshaw thanis for your help but I can't understand the use of i*i in for loop.
I'm not getting the logic plzz help me .
+ 3
To check if a number is a prime, you can check if it doesn't divide till the numbers square root.
This way saves you program runtime.