A number which is divided by one and itself is called prime . I will suggest to you first read this article how to check given number is prime or not in java . Now i am thinking you know about what is prime number and how to check prime number in java. In this post we will see how to print prime number 1 to 100 in java .

### Java program to print prime numbers between 1 to 100

In this program we will write java program to display prime number from 1 to 100 . For this we use this algorithm

1. Iterate loop 1 to 100

2. for each iterate check number prime or not

3. print number if it is prime .

```
public class PrimeNumberToRange {
public static void main(String[] args) {
System.out.println("prime number between 1 to 100 :");
for (int i = 1; i <= 100; i++) {
if (isPrimeNumber(i)) {
System.out.print(i + " ");
}
}
}
private static boolean isPrimeNumber(int num) {
if (num <= 1) {
return false;
}
boolean flag = true;
for (int j = 2; j <= Math.sqrt(num); j++) {
if (num % j == 0) {
flag = false;
break;
}
}
return flag;
}
}
```

Output :

```
prime number between 1 to 100 :
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
```

We have printed prime numbers from 1 to 100 in java using for loop. We can use while loop alsoin same algorithm .

### Java program to print prime numbers between 1 to n

print prime numbers from 1 to n in java . we will print list of prime numbers from 1 to n using for loop. n take as input from user.

```
import java.util.Scanner;
public class PrimeNumberToRange {
public static void main(String[] args) {
Scanner scan= new Scanner(System.in);
System.out.println("Enter value of n:");
int n =scan.nextInt();
System.out.println("prime number between 1 to "+ n +" : ");
for (int i = 1; i <= n; i++) {
if (isPrimeNumber(i)) {
System.out.print(i + " ");
}
}
}
private static boolean isPrimeNumber(int num) {
if (num <= 1) {
return false;
}
boolean flag = true;
for (int j = 2; j <= Math.sqrt(num); j++) {
if (num % j == 0) {
flag = false;
break;
}
}
return flag;
}
}
```

Output:

```
Enter value of n:
10
prime number between 1 to 10 :
2 3 5 7
```

In this program we have print prime number 1 to 10 . With the help of this example we can print prime numbers between 1 to 50 or print prime numbers from 1 to 1000 or print prime numbers 1 to 200 or print prime numbers from 1 to 20 etc.

### Java Program to Display Prime Numbers Between Two Intervals

Now we will write java program to print prime numbers in a given range.

```
import java.util.Scanner;
public class PrimeNumberInterval {
public static void main(String[] args) {
Scanner scan= new Scanner(System.in);
System.out.println("Enter value of low:");
int low =scan.nextInt();
System.out.println("Enter value of high:");
int high =scan.nextInt();
scan.close();
System.out.println("prime number between "+low+" to "+ high +" : ");
for (int i = low; i <= high; i++) {
if (isPrimeNumber(i)) {
System.out.print(i + " ");
}
}
}
private static boolean isPrimeNumber(int num) {
if (num <= 1) {
return false;
}
boolean flag = true;
for (int j = 2; j <= Math.sqrt(num); j++) {
if (num % j == 0) {
flag = false;
break;
}
}
return flag;
}
}
```

Output :

```
Enter value of low:
5
Enter value of high:
10
prime number between 5 to 10 :
5 7
```

java program to print prime numbers in a given range using for loop . you can write java program to print prime numbers in a given range using while loop using same logic. You can see top java programming interview questions . In this article we have learned print prime number for 1 to 100 and 1 to n or given range.