Results 1 to 4 of 4

Thread: program to check prime number in java

  1. #1
    Join Date
    May 2009
    Posts
    857

    program to check prime number in java

    Hi,
    I get this program "to check entered number is prime number or not" in my exam.I tried various method but I am not able to write this program.So I am just asking solution of this program on this forum.If any one have solution of this program please give it to me.
    Thanks in advanced.
    Last edited by Bansi_WADIA; 24-11-2009 at 10:40 PM.

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: program to check prime number in java

    I write this program just using if statement.Try to compile this program in your computer.


    Code:
     class PrimeNumber
            {
             public static void main(String[] args) 
                    {
                    int Num1 = 11;
                    int p;
                    for (p=2; pi < Num ;p++ )
                        {
                         int x = num%p;
                         if (x==0)
                           {
                            System.out.println("Entered number is not Prime!");
                             break;
                            }
                         }
                    if(p == Num)
                    {
                      System.out.println("Prime number!");
              }
      }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: program to check prime number in java

    Try following code to check prime number.


    Code:
               class PrimeNumber
                  {
    
                    public static void main(String args[])
                           {
    
                          int Num = Integer.parseInt(args[0]);
    
                          int opt=0;
    
                         for(int p=2;p<num;p++)
                           {
    
                           if(num%p==0)
    
                             {
    
                             System.out.println(Num+" is not a Prime Number");
    
                             opt = 1;
    
                             break;
    
                             }
    
                          }
    
                          if(opt==0)
    
                         System.out.println(Num+" is a Prime Number");
    
        }
    
    }

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: program to check prime number in java

    I just write program which is small but very complex.Try to understand each step.


    Code:
            public static boolean prime (long PrimeNmber)
    	{
    	    for (int x=1; x<PrimeNmber; x+=2)
    		{
    		if ((PrimeNmber%x==0 && x!=1) || (PrimeNmber%2==0 && PrimeNmber!=2))
    			{	
    			return false;
    			}
    		}
    		return true;
    	}

    I think this will help you.
    Last edited by Reegan; 24-11-2009 at 11:05 PM.

Similar Threads

  1. Problem with C# program which calculates the prime number
    By $Bird$ in forum Software Development
    Replies: 4
    Last Post: 29-11-2010, 12:54 PM
  2. java program to add leading zeros to a number.
    By kamina23 in forum Software Development
    Replies: 4
    Last Post: 05-02-2010, 07:15 PM
  3. Shell program for the prime number
    By Rup_me in forum Software Development
    Replies: 5
    Last Post: 14-12-2009, 10:24 AM
  4. program to print prime factor of given number
    By teena_pansare in forum Software Development
    Replies: 4
    Last Post: 27-11-2009, 10:21 AM
  5. Check whether an integer is a prime number in C
    By Projectkmo in forum Software Development
    Replies: 2
    Last Post: 08-10-2008, 06:27 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,123,287.28252 seconds with 16 queries