Java program to Find whether number is Prime or Not

Friday, February 4th, 2011
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");
	}
}

Author Name :
surajk

Total : 17 Comments


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

  1. nice web site for c, java source
    send vb projects please(airline resurvation system)

  2. vishal says:

    very nice budy. thanxxxxxx

  3. abdul says:

    Thanks sir.. Super website

  4. Sheth Amit says:

    good working dude………..

  5. SIVA says:

    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!

  6. poornima says:

    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.

  7. poornima says:

    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.

  8. abhida says:

    Great helpg buddyzzzzz…… thnx yar…..

  9. jaya prakash says:

    hi abitha wat dng

  10. jaya prakash says:

    you name s sexy

  11. Simple says:

    Simple and Esay

  12. Code Nirvana says:

    Here’s my way:
    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");
    }
    }

    MORE @ http://www.codenirvana.in/2013/04/how-to-check-prime-number-in-java.html

  13. Jyothi says:

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

  14. java2novice says:

    very nice. for more java examples visit java2novice.com site

  15. rehaan says:

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

  16. coopX12 says:

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

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: