# Java program to print prime numbers from 2 to N

Let’s learn java program to print prime numbers from 2 to N.

## Java program to print prime numbers from 2 to N

Prime number is one which is divisible by 1 and itself. In the below java program let’s find prime numbers from 2 to N.

First we read input from user or maximum number using nextInt() method of Scanner class and loop from 2 to N using findPrime() method.

In this findPrime() method we will find whether number is prime or not. In this for loop if condition divides any number between 2 to (number/2)-1 then number will not be prime number. Here’s program to print prime numbers from 2 to N.

```import java.util.Scanner;
{
public static void main(String[] args)
{
int loop, max;
System.out.print("Please enter highest (maximum) number: ");
Scanner sc = new Scanner(System.in);
max = sc.nextInt();
for(loop = 2; loop < max; ++loop)
{
if(findPrime(loop))
{
System.out.println(loop);
}
}
sc.close();
}
public static boolean findPrime(int number)
{
int a;
boolean boolPrime = true;
for(a = 2; a < number / 2; a++)
{
if(number%a == 0)
{
boolPrime = false;
break;
}
}
return boolPrime;
}
}
```

Output:

Please enter highest (maximum) number: 20
2
3
4
5
7
11
13
17
19

Please enter highest (maximum) number: 30
2
3
4
5
7
11
13
17
19
23
29