Java program to Find whether number is Prime or Not

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

19 thoughts on “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!

  2. 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.

  3. 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.

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

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

  6. if i enter number “1” or “0” this code is giving me that it is prime but it is not y??

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