Prime Number Program in Java Using While loop & For Loop

Prime Number: A number that is divisible only by itself and 1 is called Prime number.
Examples: 2, 3, 5, 7, 11, 13, 17, 19 etc.

Prime number programs in Java:

Program to display the prime numbers from 1 to 100
public class Prime
{
 public static void main(String[] args)
 {
  int limit = 100;
  System.out.println("Prime numbers between 1 and 100");
      for(int i=2; i < limit; i++)
     {
      boolean isPrime = true;
       for(int j=2; j < i ; j++)
        {
         if(i % j == 0)
         {
           isPrime = false;
            break;
          }
         }
     if(isPrime)
    System.out.print(i + " ");
   }
 }
}

Output:
Prime numbers between 1 and 100
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Program to display prime numbers from 1 to n:

 

import java.util.Scanner;

public class Prime2

{

 public static void main(String[] args)

 {

 Scanner s1 = new Scanner(System.in);

 System.out.println("Enter the value of N");

 int n = s1.nextInt();

    for(int i=2; i < n; i++)

     {

      boolean isPrime = true;

       for(int j=2; j < i ; j++)

        {

         if(i % j == 0)

         {

           isPrime = false;

            break;

          }

         }

     if(isPrime)

    System.out.print(i + " ");

   }

 }

}

Output:

Enter the value of N

40

2 3 5 7 11 13 17 19 23 29 31 37

Prime Number Program using Method in Java:

class Prime3

{

 void ToCheckPrime(int n)

 {

  int i,j=0,k=0;

  j = n/2;

  if(n==0||n==1)

  {

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

  }

 else

   { 

   for(i = 2;i <= j;i++)

   {     

    if(n%i == 0)

   {     

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

     k = 1;     

     break;     

    }     

   }     

   if(k == 0) 

   {

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

   } 

  }

 }

public static void main(String[] args)

 {

  Prime3 p1 = new Prime3();

  p1.ToCheckPrime(1);

  p1.ToCheckPrime(5);

  p1.ToCheckPrime(8);

 }

}

Output:

1 is not prime number

5 is prime number

8 is not prime number

Program to Check Prime Number using a while loop:

import java.util.Scanner;

class Prime4

{

public static void main(String[] args)

 {

 Scanner s1 = new Scanner(System.in);

 System.out.println("Enter the value of N");

 int n = s1.nextInt();

 int i = 2;

 boolean flag = false;

 while(i <= n/2)

 {

  if(n % i == 0)

  {

   flag = true;

   break;

  }

 ++i;

 }

 if (!flag)

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

 else

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

 }

}


Output:

Enter the value of N

15

15 is not a prime number.

Find prime numbers between two numbers:

import java.util.Scanner; 

public class Prime5

 { 

   public static void main(String[] args)

 { 

       Scanner s = new Scanner(System.in); 

       System.out.print("Enter the first number = "); 

       int firstNumber = s.nextInt(); 

       System.out.print("Enter the second number = "); 

       int secondNumber = s.nextInt(); 

       System.out.println(" The prime numbers between " + firstNumber + " and " + secondNumber); 

       for (int i = firstNumber; i <= secondNumber; i++)

           { 

           if (isPrime(i))

           { 

               System.out.println(i); 

           } 

       } 

   } 

   public static boolean isPrime(int n) { 

       if (n <= 1)

       { 

           return false; 

       } 

       for (int i = 2; i <= Math.sqrt(n); i++)

        { 

           if (n % i == 0)

           { 

               return false; 

           } 

       } 

       return true; 

   } 

  Output:

Enter the first number = 2

Enter the second number = 15

The prime numbers between 2 and 15

2

3

5

7

11

13


Share:

Ads

Search This Blog

  • ()
Powered by Blogger.

Strings in C With Syntax, C String Program with output

Strings in C :  Strings can be defined as the one-dimensional array of characters terminated by a null ('\0'). The difference betwee...