import java.util.*;
class PrimeNumbers
{
public static void main(String args[])
{
int n, status = 1, num = 3;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of prime numbers you want");
n = in.nextInt();
if (n >= 1)
{
System.out.println("First "+n+" prime numbers are :-"); System.out.println(2);
}
for ( int count = 2 ; count <=n ; )
{
for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
{
if ( num%j == 0 )
{
status = 0;
break;
}
}
if ( status != 0 )
{
System.out.println(num);
count++;
}
status = 1;
num++;
}
}
}
Code Breakdown:
1. Importing the Scanner Class:
import java.util.*;
It brings in the Scanner class because it has to read input from the user.
2. Class Declaration:
class PrimeNumbers
This declares a class named PrimeNumbers. The main method is inside this class, where the program begins its execution.
3. Main Method:
public static void main(String args[])
The main method is the program’s entry point. When the program runs, this code gets executed.
4. Variable Declaration:
int n, status = 1, num = 3;
Three integer variables are declared:
- n will store the number of prime numbers the user wants.
- status is used to track whether a number is prime (it starts as 1, assuming the number is prime).
- num is the current number being checked to see if it is prime. Initially set to 3 because 2 is already printed as the first prime number.
5. Scanner Object:
Scanner in = new Scanner(System.in);
This declaration creates a Scanner object called in that reads input from a user.
6. Getting Input from the User:
System.out.println(“Enter the number of prime numbers you want”);
n = in.nextInt();
The program prompts the user to input how many prime numbers they wish to see. The user input is stored in the variable n.
7. Handling Special Case for n >= 1:
if (n >= 1)
{
System.out.println(“First “+n+” prime numbers are :-“);
System.out.println(2);
}
If n is greater than or equal to 1, the program prints the first prime number (which is always 2). The output also indicates how many prime numbers will be displayed.
8. Finding Prime Numbers:
for (int count = 2; count <= n;)
{
for (int j = 2; j <= Math.sqrt(num); j++)
{
if (num % j == 0)
{
status = 0;
break;
}
}
if (status != 0)
{
System.out.println(num);
count++;
}
status = 1;
num++;
}
- The outer for loop (count = 2; count <= n;) starts at count = 2 because the number 2 has already been printed. It continues until the count reaches the user-defined value n, which indicates how many prime numbers are printed.
- The inner loop for (int j = 2; j <= Math.sqrt(num); j++) is a check if the number (num) is divisible by some integer ranging from 2 to square root of num. That’s a clever optimization for not doing that many divisions for a primality test.
- The number is not a prime if divisible by j (num % j == 0). Set the status to 0 and break from the loop.
- In case no divisor is found (status != 0), the number being checked, num, is prime and printed. Then the count is incremented, and the process continues to look for the next prime number.
- After checking the current number, status is reset to 1, and num is incremented to check the next number.
9. End of Program:
The loop goes ahead until the first n prime numbers are found and printed.
Input and Output Example
Input:
Enter the number of prime numbers you want
5
Output:
First 5 prime numbers are :-
2
3
5
7
11
Output Explanation
1. The user inputs 5, which means they want the first 5 prime numbers.
2. The program prints 2 as the first prime number (taken care of by the initial portion of the program).
3. The program now checks the next numbers (3, 4, 5, 6,.) and finds that 3, 5, 7, and 11 are prime, printing each of them.
4. After printing 5 prime numbers, the loop stops and the program ends.
Summary:
- The program first asks the user how many prime numbers they want.
- It then resorts to two nested for loops to test and print primes starting from 3 (as 2 is treated separately).
- The program also checks if a number is prime through the testing for divisibility only up to its square root.
- The program prints the first n primes, as given by the user.