+ 6

How to create a java program to get 10001th prime number.

I have done many attempts but failed plzz anyone help me.

23rd Oct 2017, 6:24 PM
Vaibhav Singh Sikarwar
Vaibhav Singh Sikarwar - avatar
4 ответов
+ 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 :-)
23rd Oct 2017, 7:12 PM
Qanshaw Avgana
Qanshaw Avgana - avatar
+ 8
check this java version i think you like!! https://code.sololearn.com/c8KCZTNWXzC8/?ref=app
15th Feb 2018, 6:46 PM
ASIF BILAKHIYA
ASIF BILAKHIYA - avatar
+ 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 .
24th Oct 2017, 10:24 AM
Vaibhav Singh Sikarwar
Vaibhav Singh Sikarwar - avatar
+ 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.
24th Oct 2017, 3:35 PM
Qanshaw Avgana
Qanshaw Avgana - avatar