Java program to print the prime numbers

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

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

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

6 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);
    }
    }
    }

    Reply
  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);
    }

    }

    }
    }

    Reply
  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
    {
    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.");
    }
    }

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

    }}

    Reply

Leave a Reply