# How to print prime numbers in java

##### How to print prime numbers in java

There are several ways of writing a program in Java that checks whether a number is prime on not. However, the basic logic remains the same i.e; you need to check whether the entered number (or already defined number in the program) has some divisor other than one and itself or not.

The Definition and Importance (Prime Numbers):

The number which is only divisible by itself and 1 is known as prime number. For example 2, 3, 5, 7â€¦are prime numbers. Here we will see two programs: 1) First program will print the prime numbers between 1 and 100 2) Second program takes the value of n (entered by user) and prints the prime numbers between 1 and n.

Example 1:

A prime number is a number that is divisible by only two numbers: 1 and itself. So, if any number is divisible by any other number, it is not a prime number.

<strong>Method 1:</strong> Program to Check Prime Number using a for loop

public class Main {

public static void main(String[] args) {

``````int num = 29;
boolean flag = false;
for (int i = 2; i <= num / 2; ++i) {
// condition for nonprime number
if (num % i == 0) {
flag = true;
break;
}
}

if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");``````

}
}

Output:

29 is a prime number.

Example 2:

In above example, we find the prime number using for loop. Let’s try to find the prime number using while loop.

<strong>Method 2:</strong> Program to Check Prime Number using a while loop

public class Main {

public static void main(String[] args) {

``````int num = 33, i = 2;
boolean flag = false;
while (i <= num / 2) {
// condition for nonprime number
if (num % i == 0) {
flag = true;
break;
}

++i;
}

if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");``````

}
}

Output:

33 is not a prime number.

Example 3:

It will display the prime numbers between 1 and 100.

<strong>Method 3:</strong> Program to display the prime numbers from 1 to 100
```class PrimeNumbers
{
public static void main (String[] args)
{
int i =0;
int num =0;
//Empty String

for (i = 1; i <= 100; i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
//Appended the Prime number to the String
}
}
System.out.println("Prime numbers from 1 to 100 are :");
}
}```

Output:

Prime numbers from 1 to 100 are : 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

Example 4:

It will display all the prime numbers between 1 and n (n is the number, entered by user).

<strong>Method 4:</strong> Program to display prime numbers from 1 to n
```import java.util.Scanner;
{
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
int i =0;
int num =0;
//Empty String
System.out.println("Enter the value of n:");
int n = scanner.nextInt();
scanner.close();
for (i = 1; i <= n; i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
//Appended the Prime number to the String