# Print First N Prime Numbers – Java Code

Objective: Given a number N, write a program to print first N prime numbers.

What is Prime number:??
A prime number (or a prime) is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. A number is either divisible by 1 or by number by itself.

Example:

```N = 5
2 3 5 7 11

N = 10
2 3 5 7 11 13 17 19 23 29
```

Approach:

• Start from number 2 to till we find N prime numbers
• Check for each number if it is prime, if yes then increment the prime count.
• Check this – Check if Given number is Prime
• Check the code, its self explanatory.

Code:

Output:

```2 3 5 7 11
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71```

__________________________________________________
Top Companies Interview Questions..-

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

### 2 Responses

1. lipsa patel says:

In the example for N = 10
11 numbers are printed instead of 10

2 3 5 7 11 13 17 19 23 29 31

• tutorialhorizon says:

Thanks lipsa. Modified the example

This site uses Akismet to reduce spam. Learn how your comment data is processed.