Prime number java

Hi guys!! Welcome to flower brackets blog. Today we are going to learn prime number java.

Before going to find prime number in java, let us understand what is prime number and how to find prime numbers in java?

Prime Number Java

What is prime number?

A prime number in java is a number that is greater than one and divided by itself.

Prime numbers are those numbers which cannot be divided by other numbers than 1 or itself.

Also Read – Palindrome Number In Java

Example : 2, 3, 5, 7, 11, 13, 17….

Example: prime number java

Here’s an example for prime number program in java,

import java.util.*;

public class JavaPrimeNumber
{
   public static void main(String[] args)
   {
      int digit;
      boolean checkPrime = true;
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter any number find to prime number in java : "); 
      int number = sc.nextInt();
 
      for(int a = 2; a <= number / 2; a++)
      {
         digit = number % a;
         if(digit == 0)
         {
            checkPrime = false;
            break;
         }
      }
 
      if(checkPrime)
         System.out.println(number + " is a prime number.");
      else
         System.out.println(number + " is not a prime number.");
      sc.close();
   }
}

Output :

Enter any number to find prime number in java : 14
14 is not a prime number.


Now let’s see how to find prime numbers in java using while loop,

import java.util.Scanner;

public class JavaPrimeNumber 
{
   public static void main(String[] args) 
   { 
      boolean checkPrime = true;
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter any number find prime number in java : "); 
      int number = sc.nextInt();
 
      int a = 2;
      while(a <= number / 2)
      {
         if(number % a == 0)
         {
            checkPrime = false;
            break;
         }
         a++;
      }
 
      if(checkPrime)
         System.out.println(number + " is a prime number.");
      else
         System.out.println(number + " is not a prime number.");
      sc.close();
   }
}

Output:

Enter any number find prime number in java : 50
50 is not a prime number.


Here in this java program we are going to learn how to write a program that prints the numbers from 1 to 100,

public class PrimeNumbersJava 
{
   public static void main(String[] args) 
   {
      int a = 0;
      int number = 0;
      String strPrimeNumber = "";

      for(a = 1; a <= 100; a++) 
      { 
         int counter = 0; 
         for(number = a; number >= 1; number--)
         {
            if(a % number == 0)
            {
               counter = counter + 1;
            }
         }
         if (counter == 2)
         { 
            strPrimeNumber = strPrimeNumber + a + " ";
         } 
      } 
      System.out.println("Prime numbers from 1 to 100 in java : ");
      System.out.println(strPrimeNumber);
   }
}

Output :

Prime numbers from 1 to 100 in java : 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


Now let’s learn how to find prime numbers from 1 to n in java program. In this program the value ‘n’ will be entered by the user.

For this we are using java.util.Scanner class,

import java.util.Scanner;

public class PrimeNumber 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      int a = 0;
      int number = 0; 
      String strPrimeNumber = "";
      System.out.println("Please enter the value of n : ");
      int n = sc.nextInt();
 
      for(a = 1; a <= n; a++) 
      { 
         int count = 0; 
         for(number = a; number >= 1; number--)
         {
            if(a % number == 0)
            {
               count = count + 1;
            }
         }
         if(count == 2)
         {
            strPrimeNumber = strPrimeNumber + a + " ";
         } 
      } 
      System.out.println("Prime numbers from 1 to " + n + " in java are : ");
      System.out.println(strPrimeNumber);
      sc.close();
   }
}

Output:

Please enter the value of n : 200
Prime numbers from 1 to 200 in java are :
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 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199


Prime number java: java check if number is prime

In this segment we will learn prime number program in java and check prime number in java,

Method #1

public class JavaPrimeNumbers 
{
   public static void main(String[] args) 
   {
      int a, b = 0, temp = 0; 
      // this is the number we are going to check
      int number = 5; 
      b = number / 2; 
      if(number == 0 || number == 1)
      { 
         System.out.println(number + " is not prime number."); 
      }
      else
      { 
         for(a = 2; a <= b; a++)
         { 
            if(number % a == 0)
            { 
               System.out.println(number + " is not prime number."); 
               temp = 1; 
               break; 
            } 
         } 
         if(temp == 0) 
         { 
            System.out.println(number + " is prime number."); 
         } 
      }
   }
}

Output :

5 is prime number.


Method #2

So now let’s see a java program to check if a number is prime in java,

public class CheckPrimeNumberJava 
{
   static void checkPrimeNumber(int number)
   { 
      int a, b = 0, temp = 0; 
      b = number / 2; 
      if(number == 0 || number == 1)
      { 
         System.out.println(number + " is not a prime number."); 
      }
      else
      { 
         for(a = 2; a <= b; a++)
         { 
            if(number % a == 0)
            { 
               System.out.println(number + " is not a prime number."); 
               temp = 1; 
               break; 
            } 
         } 
         if(temp == 0)
         {
            System.out.println(number + " is a prime number."); 
         } 
      } 
   }
 
   public static void main(String[] args) 
   {
      checkPrimeNumber(2); 
      checkPrimeNumber(5); 
      checkPrimeNumber(11); 
      checkPrimeNumber(18); 
   }
}

Output:

2 is a prime number.
5 is a prime number.
11 is a prime number.
18 is not a prime number.


Method #3

Let’s check if a number is prime java using scanner class,

import java.util.Scanner;

public class JavaPrimes 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in); 
      System.out.print("Please enter a number to check if number is prime in java : "); 
      int num = sc.nextInt(); 
      if(checkPrimeNumber(num)) 
      { 
         System.out.println(num + " is a prime number."); 
      }
      else
      { 
         System.out.println(num + " is not a prime number."); 
      }
      sc.close();
   }
 
   public static boolean checkPrimeNUmber(int number) 
   { 
      if(number <= 1) 
      { 
         return false; 
      } 
      for(int a = 2; a < Math.sqrt(number); a++) 
      { 
         if(number % a == 0) 
         { 
            return false; 
         } 
      } 
      return true;
   }
}

Output :

Please enter a number to check if number is prime in java : 25
25 is a prime number.


Method #4 – between two numbers

How to check if number is prime in java between range of two numbers,

public class PrimeNumbers 
{
   boolean checkPrime(int number)
   {
      int count = 0;
      for(int a = 1; a <= number; a++)
      {
         if(number % a == 0)
         {
            count++;
         }
      }
      if(count == 2)
      {
         return true;
      }
      else
      {
         return false;
      }
   }
}

import java.util.*;

public class PrimeNumberRange 
{
   public static void main(String[] args) 
   {
      PrimeNumbers prime = new PrimeNumbers();
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter the lower range : ");
      int p = sc.nextInt();
      System.out.println("Please enter the upper range : ");
      int q = sc.nextInt();
 
      if(p > q)
      {
         System.out.println("Invalid range");
      }
      else
      {
         System.out.println("Twin prime numbers within the given range.");
         for(int i = p; i <= (q - 2); i++)
         {
            if(prime.checkPrime(i) == true && prime.checkPrime(i + 2) == true)
            {
               System.out.print("(" + i + "," + (i + 2) + ") ");
            }
         }
      }
      sc.close();
   }
}

Output :

Please enter the lower range : 1
Please enter the upper range : 100
Twin prime numbers within the given range.
(3,5) (5,7) (11,13) (17,19) (29,31) (41,43) (59,61) (71,73)


conclusion

That’s it guys. This is all about how to find prime numbers in java. You can subscribe to my blog flower brackets if you haven’t already.

I hope you have understood how to find prime numbers from 1 to 100 and how to find prime numbers from 1 to n java program.

Do share this post if you like.

Related Posts