Friday, 10 August 2018

what is prime number and prime number java program


what is prime number?
That number divisible only one and itself called prime number.In other words we say
a prime number is whole number greater than one whose only factors are one and itself.
only 2 is a smallest even prime number some first prime number are 2,3,5,7,11,13,17,19 etc.

Program to display the prime numbers from 1 to 100


It will display the prime numbers between 1 and 100.
class PrimeNumbers
{
   public static void main (String[] args)
   {
       int i =0;
       int num =0;
       //Empty String
       String  primeNumbers = "";

       for (i = 1; i <= 100; i++)         
       {     
          int counter=0;   
          for(num =i; num>=1; num--)
  {
             if(i%num==0)
     {
  counter = counter + 1;
     }
  }
  if (counter ==2)
  {
     //Appended the Prime number to the String
     primeNumbers = primeNumbers + i + " ";
  }
       }
       System.out.println("Prime numbers from 1 to 100 are :");
       System.out.println(primeNumbers);
   }
}

Program to display prime numbers from 1 to n


It will display all the prime numbers between 1 and n (n is the number, entered by user).

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package prime;
import java.util.Scanner;

/**
 *
 * @author rajesh kumar shukla
 */
public class Prime {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner scanner = new Scanner(System.in);
      int i =0;
      int num =0;
      //Empty String
      String  primeNumbers = "";
      System.out.println("Enter the value of n:");
      int n = scanner.nextInt();
      scanner.close();
      for (i = 1; i <= n; i++)     
      {   
         int counter=0;   
         for(num =i; num>=1; num--)
         {
    if(i%num==0)
    {
counter = counter + 1;
    }
}
if (counter ==2)
{
    //Appended the Prime number to the String
    primeNumbers = primeNumbers + i + " ";
}
      }
      System.out.println("Prime numbers from 1 to n are :");
      System.out.println(primeNumbers);
   }
    }
    



Example 1: Program to Check Prime Number using a for loop

public class Prime {

    public static void main(String[] args) {

        int num = 29;
        boolean flag = false;
        for(int i = 2; i <= num/2; ++i)
        {
            // condition for nonprime number
            if(num % i == 0)
            {
                flag = true;
                break;
            }
        }

        if (!flag)
            System.out.println(num + " is a prime number.");
        else
            System.out.println(num + " is not a prime number.");
    }
}

Example 2: Program to Check Prime Number using a while loop

public class Prime {

    public static void main(String[] args) {

        int num = 33, i = 2;
        boolean flag = false;
        while(i <= num/2)
        {
            // condition for nonprime number
            if(num % i == 0)
            {
                flag = true;
                break;
            }

            ++i;
        }

        if (!flag)
            System.out.println(num + " is a prime number.");
        else
            System.out.println(num + " is not a prime number.");
    }
}

Example: Display Prime Numbers Between two Intervals

public class Prime {

    public static void main(String[] args) {

        int low = 20, high = 50;

        while (low < high) {
            boolean flag = false;

            for(int i = 2; i <= low/2; ++i) {
                // condition for nonprime number
                if(low % i == 0) {
                    flag = true;
                    break;
                }
            }

            if (!flag)
                System.out.print(low + " ");

            ++low;
        }
    }
}

Example: Prime Numbers Between Two Integers

public class Prime {

    public static void main(String[] args) {

        int low = 20, high = 50;

        while (low < high) {
            if(checkPrimeNumber(low))
                System.out.print(low + " ");

            ++low;
        }
    }

    public static boolean checkPrimeNumber(int num) {
        boolean flag = true;

        for(int i = 2; i <= num/2; ++i) {

            if(num % i == 0) {
                flag = false;
                break;
            }
        }

        return flag;
    }
}

0 comments:

Post a Comment