Subscribe Us

Prime Number In Java

Prime Number

Java Program To Check Prime Number or Not

A prime number is a whole number greater than 1 whose only factors are 1 and itself.
A factor is a whole number that can be divided evenly into another number.
The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29.
Numbers that have more than two factors are called composite numbers.

Note: The number 1 is neither prime nor composite.

Example : Given Number : 11
It is a Prime Number
Since :
11 whose only factors are 1 and itself (11).
Given Number : 10
It is Not a Prime Number
Since :
10 Have more than two factors are called composite numbers

To check if a number is a Prime Number in Java
you can use the following algorithm:
  1. Read the input number.
  2. Iterate from 1 to the input number.
    • For each iteration,
      check if the number is divisible by the current iteration value.
    • If it is divisible by the current iteration value.
      Increase count by 1
  3. Check value of count = 2, If it is Equal to 2
    It is Prime Number otherwise It is Not a Prime Number(Composite Number)
  4. Print the result.

In this algorithm,It iterates from 1 to the number and checks if the number is divisible by any value in that range. If it is divisible, increase the value of count by 1 Check value of count = 2, If it is Equal to 2,It is Prime Number otherwise It is Not a Prime Number(Composite Number)

Please note that this implementation assumes that the input number is a positive integer. You may need to add additional checks for negative numbers or other cases depending on your requirements.

Here is an example implementation in Java

Source Code

// Java Program To Check Prime Number Or Not(Composite) ? - Part One
import java.util.*;
public class Prime
  {
    public static void main(String args[])
      {
        Scanner in = new Scanner(System.in);
        int num ,i ,fact=0;
        System.out.print("Enter a Number To Check : ");
        num = in.nextInt();
        for(i=1;i<=num;i++)
          {
            if(num%i==0)
              {
               System.out.print(i+" ");   
               fact++;
              } 
          }
        //  New Line After Printing The Factors of Given Number    
        System.out.println(); 
        if(fact==2)
          System.out.print("It is a Prime Number . ");
        else
        //System.out.print("It is a Composite Number . ");
          System.out.print("It is Not a Prime Number . ");
      }
  }

Output

Sample Input :
Enter a Number To Check : 11
Sample Output:
1 11
It is a Prime Number

Sample Input :
Enter a Number To Check : 10
Sample Output:
1 2 5 10
It is Not a Prime Number

To check if a number is a Prime Number in Java
you can use the following algorithm:
  1. Read the input number.
  2. initialize check to 0
  3. Iterate from 2 to half of the input number.
    • For each iteration,
      check if the number is divisible by the current iteration value.
    • If it is divisible by the current iteration value.
      initialize check to 1 and break the loop
  4. Check value of check = 0, If it is Equal to 0
    It is Prime Number otherwise It is Not a Prime Number(Composite Number)
  5. Print the result.

In this algorithm,It iterates from 2 to half of the number and checks if the number is divisible by any value in that range. If it is divisible, initialize check to 1 and break the loop, Check value of check = 0, If it is Equal to 0,It is Prime Number otherwise It is Not a Prime Number(Composite Number)

Please note that this implementation assumes that the input number is a positive integer. You may need to add additional checks for negative numbers or other cases depending on your requirements.

Here is an example implementation in Java

Source Code

// Java Program To Check Prime Number Or Not(Composite) ? - Part Two
import java.util.*;
public class Prime
  {
    public static void main(String args[])
      {
        Scanner in = new Scanner(System.in);
        int num ,i;
        boolean check=true;
        System.out.print("Enter a Number To Check : ");
        num = in.nextInt();
        for(i=2;i<=num/2;i++)
          {
            if(num%i==0)
              {
               check=false;
               break;
              } 
          }
        // if(check)  
        if(check==true)
          System.out.print("It is a Prime Number . ");
        else
        //System.out.print("It is a Composite Number . ");
          System.out.print("It is Not a Prime Number . ");
      }
  }

Output

Sample Input :
Enter a Number To Check : 11
Sample Output:
It is a Prime Number

Sample Input :
Enter a Number To Check : 10
Sample Output:
It is Not a Prime Number