# Java program to display prime numbers from 1 to 100

Let’s learn prime number program in java and java program to display prime numbers from 1 to 100.

Java program to find prime numbers is most common java interview question.

#### Java program to display prime numbers from 1 to 100

A prime number is one which is divisible by 1 and itself. Here’s the prime number from 1 to 100 program in java,

```// java code for prime number
{
public static void main(String[] args)
{
int a = 0;
int number = 0;
String strPrimeNumber = "";
for(a = 1; a <= 100; a++)
{
int counter = 0;
for(number = a; number >= 1; number--)
{
if(a % number == 0)
{
counter = counter + 1;
}
}
if(counter == 2)
{
strPrimeNumber = strPrimeNumber + a + " ";
}
}
System.out.println("Prime numbers from 1 to 100 : ");
}
}

```

Output:

Prime numbers from 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

Similarly we can display from 1 to n. Here’s the java code for prime number,

```import java.util.Scanner;

{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int a = 0;
int number = 0;
String strPrimeNumber = "";
System.out.println("Please enter the value of n : ");
int n = sc.nextInt();
for(a = 1; a <= n; a++)
{
int count = 0;
for(number = a; number >= 1; number--)
{
if(a % number == 0)
{
count = count + 1;
}
}
if(count == 2)
{
strPrimeNumber = strPrimeNumber + a + " ";
}
}
System.out.println("Prime numbers from 1 to n : ");