# Java program to print the prime numbers

Write a Java program that prompts the user for an integer and then prints out all prime numbers up to that integer.

```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import java.util.Scanner; class prime { public static void main(String[] args) { int n,p; Scanner s=new Scanner(System.in); System.out.println(“Enter upto which number prime numbers are needed”); n=s.nextInt(); for(int i=2;i<n;i++) { p=0; for(int j=2;j<i;j++) { if(i%j==0) p=1; } if(p==0) System.out.println(i); } } }```

Output:

Enter upto which number prime numbers are needed:20
2
3
5
7
11
13
17
19

Enter up to which number prime numbers are needed:35

Start exploring endless computing possibilities with your own Raspberry Pi computer and accessories. Perfect for beginners and students.

2
3
5
7
11
13
17
19
23
29
31

### 14 Responses to “Java program to print the prime numbers”

1. Rajib Ghosh

import java.util.Scanner;
class prime
{
public static void main(String[] args)
{
int n,p;
Scanner s=new Scanner(System.in);
System.out.println(“Enter upto which number prime numbers are needed”);
n=s.nextInt();
for(int i=2;i<n;i++)
{
p=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
p=1;
}
if(p==0)
System.out.println(i);
}
}
}

2. import java.util.*;

class Prime
{
public static void main(String str[])
{

Scanner in=new Scanner(System.in);
System.out.println(“Enter number upto which you want to find prime numbers: “);
int n=in.nextInt();
System.out.println(“The prime numbers are:”);
for(int i=2;i<=n;i++)
{
int k=0;
for(int j=2;j<=i;j++)
{
if(i%j==0){
k=k+1;
}
}
if(k==1){
System.out.println(i);
}

}

}
}

3. Harsh

Java source code to print prime unique numbers between two limits.
import java.io.*;
class primeUniqueNo
{
static int check(int a)
{
int b;
for(b = 2; b <= a/2; b++)
{
if(a%b==0)
{
return 0;
}
}
return 1;
}
long countDigits(long no)
{
return String.valueOf(no).length();
}
boolean checkUnique(long n)
{
int nd = (int)countDigits(n);
long arr[] = new long[nd];
long copy = n;
int digit,term = 0;
while(copy!=0)
{
digit = (int)copy%10;
arr[term] = digit;
term++;
copy/=10;
}
for(int i = 0;i<nd-1;i++)
{
for(int j = i+1;j<nd;j++)
{
if(arr[i] == arr[j])
return false;
}
}
return true;
}
void main() throws IOException
{
System.out.println("Enter limits");
int n = Integer.parseInt(br.readLine());
int s = Integer.parseInt(br.readLine());
int count = 0;
for(int i = n;i<=s;i++)
{
if(checkUnique(i)==true)
{
if(check(i)==1)
{
System.out.print(i + " ");
count++;
}
}
}
System.out.println("");
System.out.println(count+" prime unique number(s) had been found.");
}
}

4. import java.io.*;
import java.util.*;

class prime
{
public static void main(String args[])throws IOException
{

/* Check if a number is prime or not */
int x=0;
int n,m;

System.out.print(“Enter a no. : “);

for(int i=2; i<=n/2; i++)
{
m = n%i;
if(m==0)
{ x++; }
}
if(x==0)
System.out.println("The number is a prime no. ");
else System.out.println("It is not prime no. ");

/* Print List of prime nos. till N'th number */

int a,b;
int c;
int d;
System.out.println("Enter number till which you want prime no.:");

for(a=2;a<d;a++)
{
c=0;
for(b=2;b<a;b++)
{
if((a%b)==0)
{
c=1;
}
}
if(c==0)
{
System.out.print(a+" ");
}
}

}}

5. public class Checkprime {
public boolean prime(int num){
for(int i=2;i<=num-1;i++){
if(num%i==0){
return false;
}
}

return true;

}

public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println("enter the range");
int range = scn.nextInt();
Checkprime p = new Checkprime();
for(int j=0;j<=range;j++){
if(p.prime(j)){
System.out.println(j);

}
}
}

}

6. Kceejones

Pls i need a program to input the number of sentence and then display:word and it frequecy

7. I need the java program ro display prime numbers upto n values using awt package

8. Pritam Mandal

import java.io.*;
class Prime
{
public static void main(String args[])throws IOException
{
System. out. println(“enter a number upto which u want to print the prime nos.);
int I,j,f;
for(I=1;I<=n;i++)
{
f=0;
for(j=1;j<=I;j++);
{
if(i%j==0)
f=f+1;
}
if(f==2)
System.out.println(I);
}
}
}