Monday, August 4, 2014

How to find the given number is prime in java

How to find the given number is prime in java
  class Testprime
  public static void main(String  args[])
   {
   System.out.println("Enter the any number..");
   String number;
   int f=0;
    int n=Integer.parseInt(number);
   for(int i=1;i<=n;i++)
   {
      if(n%i==0)
       {
                 f=f+1;
      }
  }
if(f==2)
System.out.println("The Given number is prime "+n);
  else
System.out.println("The Given number is Not prime "+n);
    }  
}

 output:
   Enter the any number 5
      The Given number is prime 5
 because  the prime has only two factors is called prime number. i.e the prime number divide only 1 and itself is called the prime number
      so 5 is the prime number because 5 is not divide any number .

    Again compile
       Enter the any number 6
      The Given number is not prime 6
  so  6 is not prime because 6 is divide by the 2 and 3.
     

   

No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...