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

2
3
5
7
11
13
17
19
23
29
31

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

10 thoughts on “Java program to print the prime numbers

  1. 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);
    }

    }

    }
    }

  2. 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
    {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    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.");
    }
    }

  3. import java.io.*;
    import java.util.*;

    class prime
    {
    public static void main(String args[])throws IOException
    {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

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

    System.out.print(“Enter a no. : “);
    n= Integer.parseInt(br.readLine());

    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.:");
    d= Integer.parseInt(br.readLine());

    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+" ");
    }
    }

    }}

  4. 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);

    }
    }
    }

    }

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in