Featured

    Featured Posts

Program to Check Whether a Number is Prime or Not

v What us Prime Number

 §  If a whole number greater than 1, is divisible by the 1 and itself then it is known as Prime Number.

 §  A prime number is a number which is divisible by only two numbers: 1 and itself. So, if any number is divisible by any other number, it is not a prime number.

Different View of the Logic

 §  The number cannot divisible by any number between 2 to n-1


Example-1:

import java.util.Scanner;

public class PrimeNumber {



     static boolean isPrime(int n) {

              boolean flag = false;

              if(n>1) {

              for(int i=2;i<=n-1;i++) {

            // condition for nonprime number

                        if(n%i==0) {

                                  flag=true;

                                  break;

                        }

              }

              }

              if(!flag)

                        return true;

              else

                        return false;

     }

     public static void main(String[] args) {

              // TODO Auto-generated method stub

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter number");

    int num=sc.nextInt();

    boolean res=isPrime(num);

    if (res)

        System.out.println(num + " is a prime number.");

    else

        System.out.println(num + " is not a prime number.");

     }

}



Example-2:

§  The number can’t divisible by any number between 2 to n/2.

import java.util.Scanner;

public class PrimeNumber {

          static boolean isPrime(int n) {

                    boolean flag = false;

                             int i=2;

                  while(i <= n/2)

                  {

                      // condition for nonprime number

                      if(n% i == 0)

                      {

                          flag = true;

                          break;

                      }



                      ++i;

                  }



                  if (!flag)

                      return true;

                  else

                      return false;

              }

         

                   public static void main(String[] args) {

                   // TODO Auto-generated method s*- nhgtub

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter number");

    int num=sc.nextInt();

    boolean res=isPrime(num);

    if (res)

        System.out.println(num + " is a prime number.");

    else

        System.out.println(num + " is not a prime number.");



          }

}

Example-3:

§  The number can’t divisible by any number between 2 to sqrt(n)

import java.util.Scanner;



public class PrimeNumber {



          static boolean isPrime(int n) {

                    boolean flag = false;

                             int i=2;

                  while(i <= Math.sqrt(n))

                  {

                      // condition for nonprime number

                      if(n% i == 0)

                      {

                          flag = true;

                          break;

                      }



                      ++i;

                  }



                  if (!flag)

                      return true;

                  else

                      return false;

              }

         

                   public static void main(String[] args) {

                   // TODO Auto-generated method stub

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter number");

    int num=sc.nextInt();

    boolean res=isPrime(num);

    if (res)

        System.out.println(num + " is a prime number.");

    else

        System.out.println(num + " is not a prime number.");



          }

}

Example-4:

§  A number which having only two factors is known as prime number

for e.g.: -

3 having two factor 1 ,3 so 3 is a prime number

6 having more than two factor 1,2,3,6 so 6 is not a prime number.

import java.util.Scanner;



public class PrimeNumber {



          static int factorCount(int n) {

                   int count=0;

                   for(int i=1;i<=n;i++) {

                             if(n%i==0)

                                      count++;

                   }

                   return count;

          }

          static boolean isPrime(int n) {

                   int c=factorCount(n);

                   if(c==2)

                             return true;

                   else

                             return false;

                            

          }

          public static void main(String[] args) {

                   // TODO Auto-generated method stub

Scanner sc=new Scanner(System.in);

System.out.println("Enter number");

int n=sc.nextInt();

boolean res=isPrime(n);

if(res==true)

          System.out.println(n+" is a Prime Number!");

else

          System.out.println(n+" is not a Prime Number!");

          }



}


www.CodeNirvana.in

Powered by Blogger.

About

Site Links

Popular Posts

Translate

Total Pageviews