The statement is: Write a program that reads an integer N and prints the first N prime numbers.
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int x = 2;
for(int i = 0; i <= N; i++)
{
int count = 0;
for(int j = 1; j <= x; j++)
if(x%j == 0)
count++;
if(count == 2)
System.out.print(x + " ");
x++;
}
}
When I run this code it's not giving me the exact N numbers. For example for N=1 & 2 it's printing the first 2 prime numbers, for N = 3 & 4, it's printing the first 3 prime numbers, for N = 5 & 6, it's printing the first 4 prime numbers, and so on. What is the problem with this code?