Java program to Find whether number is Prime or Not

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class PrimeNo{
	public static void main(String args[]){
		int num = Integer.parseInt(args[0]);
		int flag=0;
		for(int i=2;i<num;i++){
			if(num%i==0)
			{
				System.out.println(num+" is not a Prime Number");
				flag = 1;
				break;
			}
		}
		if(flag==0)
			System.out.println(num+" is a Prime Number");
	}
}

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

17 Responses to “Java program to Find whether number is Prime or Not”

  1. import java.io.*;
    public class Usersfriendly
    {
    private static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    public static void main(String args[])throws IOException
    {
    int choice;
    do
    {
    System.out.println(“INPUT YOUR NUMBER”);
    int m = Integer.parseInt(stdin.readLine());
    System.out.println(“1.PRIME NUMBERS”);
    System.out.println(“2.REAL NUMBERS”);
    System.out.println(“EXIT FROM WIZARD”);
    System.out.println(“ENTER THE CHOSEN”);
    choice = Integer.parseInt(stdin.readLine());
    switch(choice);

    {
    case1:
    int flag=0;
    for(i=2;i<m;i++)
    {
    if(m%i==0)
    {
    System.out.println(m+"IS NOT PRIME");
    flag=1;
    break;
    }
    }
    if(flag==0)
    System.out.println(m+"IS PRIME!");

    case2:
    flag=0;
    {
    if(m%2==0)
    {
    System.out.println(m+"IS EVEN");
    flag=1;
    break;
    }
    }

    if(flag==0)
    System.out.println(m+"IS ODD")
    }
    case 3:break;
    default: System.out.println("ERRORRRR!");
    while (choice!=3);
    }

    }
    }
    any mistake in this program ?
    pls give suggestions!

    Reply
  2. poornima

    Hi,
    After switch u had a ‘;’
    case should be followed by a blank space
    n should be declared
    and you used an adiitional } in case 2
    while should be written after a {
    try it after modifying these.

    Reply
  3. poornima

    Hi,
    After switch u had a ‘;’
    case should be followed by a blank space
    n should be declared
    and you used an adiitional } in case 2
    while should be written after the }
    try it after modifying these.

    Reply
  4. hai thank you program and wat happend if we use any other variable other than flag?

    Reply
  5. thnx 4 nothin coz dis programm is gvn
    “Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
    at primeno.main(primeno.java:7)

    Reply
  6. It can be done in a much much easy way:

    import java.io.*;
    class PrimeOrNot
    {
    public static void main() throws IOException
    {
    InputStreamReader ir = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(ir);
    System.out.println(“Enter the number”);
    double n = Double.parseDouble(br.readLine());
    System.out.println();
    int k, l=0;
    for (k=1; k<=n; k++)
    {
    if(n%k==0)
    {
    l=l+1;
    }
    }
    if(l==2)
    {
    System.out.println();
    System.out.println(n+" is a prime number");
    }
    else
    {
    System.out.println();
    System.out.println(n+" is NOT a prime number");
    }
    }
    }

    Reply

Leave a Reply