Prime number in Java - Java program to check prime number

Java program to check prime number

A number that has only two factors 1 and by itself or we can say a number that is divisible by 1 and by itself. Say 5, it is divisible by 1, and by itself, that is 5 is called a Prime number.

 

Prime number in Java

In the following Java code, we have to check a number is prime or not by dividing the number by 1 to the number itself. If the counter is equal to 2 then the entered number is prime otherwise not prime.

import java.util.*;

public class PrimeNumberTwofactor

{

    public static void main(String args[])

    {

        Scanner sc=new Scanner(System.in);

        int number, i, counter=0;

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

        number=sc.nextInt();

        for(i=1; i<=number; i++)

        {

            if(number % i == 0)

            {

                counter++;

            }

        }

        if(counter == 2)

        {

            System.out.println("Prime Number");

        }

        else

        {

            System.out.println("Not a Prime Number");

        }

    }

}

Another Version of Java Prime Number

package Javaclass;

import java.util.Scanner;

public class PrimeNumber {


public static void main(String[] args) {

int num, i, flag=1;

Scanner sc=new Scanner(System.in);

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

num=sc.nextInt();

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

{

if(num % i == 0)

{

flag=0;

break;

}

}

if(flag==1) {

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

}

else

{

System.out.println("Not Prime number");

}

}

}


Prime number in Java with the method

In the following code, the prime will be checked with a prime() method. One number is passed through the prime(number) in the main() method by the user and after checking the number is divisible by other than 1 and the number itself. If divisible it will return 0 other 1.

import java.util.*;

public class PrimeNumber

{

    int i, num, flag;

    int prime(int n)

    {

        flag = 1;

        num=n;

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

        {

            if(num % i == 0)

            {

                flag = 0;

                break;

            }

        }

        return flag; // flag variable return 1 or 0

    }

    public static void main(String args[])

    {

        Scanner sc=new Scanner(System.in);

        int number;

        int f;

        PrimeNumber ob = new PrimeNumber(); // object of the class

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

        number=sc.nextInt();

        f=ob.prime(number);

        if(f == 1) // check the flag variable 1 / 0

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

        else

            System.out.println("Not a Prime number");

    }

}

Prime number in Java with a method with boolean return

In the following piece of code, the prime will be checked with a prime () method and return a value boolean. One number is passed through the prime (number) in the main() method by the user and after checking the number is divisible by other than 1 and the number itself. If divisible it will return false other true.

 import java.util.*;

public class PrimeNumberCheck

{

    int i, num;     // instance varaible

    boolean flag;   // flag variable (instance variable)

    boolean prime(int n)

    {

        flag = true;

        num = n;

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

        {

            if(num % i == 0)

            {

                flag = false;

                break;

            }

        }

        return flag;

    }

 

    public static void main(String args[])

    {

        Scanner sc=new Scanner(System.in);

        int number;

        boolean f1; // boolean variable

        // object of class PrimeNumberCheck

        PrimeNumberCheck ob=new PrimeNumberCheck();

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

        number = sc.nextInt();

        f1 = ob.prime(number);      

        if(f1 == true ) // if 1 then prime or not prime

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

        else

            System.out.println("Not a Prime number");

    }

}

 

More Java program

 


SHARE THIS
Previous Post
Next Post