Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 24-11-2009
Member
 
Join Date: May 2009
Posts: 856
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.
Reply With Quote
  #2  
Old 24-11-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
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!");
          }
  }
}
Reply With Quote
  #3  
Old 24-11-2009
Member
 
Join Date: May 2008
Posts: 2,293
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");

    }

}
Reply With Quote
  #4  
Old 24-11-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 01:44 PM.