**Prime Number Definition**

**“A whole number greater than 1 that is divisible only by itself and 1”.**A prime number will always have factors as 1 and itself. A factor is a whole numbers that can be divided evenly into another number. If Number is having more than two factors are called composite numbers.

The number 1 is neither prime nor a composite Number.

Smallest prime number is 2 and others are in sequence as 2,3,5,7,11,13,17,19,13,29 etc…

**Benefit**

- prime numbers are very useful in cryptography.
- The mathematicians used prime numbers to develop new areas of mathematics, like number theory and knot theory, which developers use today.

This java program prints number of prime numbers as required from the user.

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++; } } }

## Output

Enter the number of prime numbers you want 5 First 5 prime numbers are :- 2 3 5 7 11 Enter the number of prime numbers you want 10 First 10 prime numbers are :- 2 3 5 7 11 13 17 19 23 29

## More

For more Algorithms and Java Programing Test questions and sample code follow below links

## One thought on “[Java] Prime Numbers Java program”