India's Largest Free Online Education Platform

Java program to check prime number

Java program to check prime number

Code:

import static java.lang.System.out;
import java.util.Scanner;
public class Prime_no
{
    public static void main(String args[])
    {   int flag=0;
        out.println("Enter a number to check it is prime or not");
        Scanner Sc= new Scanner(System.in);
        int x= Sc.nextInt();

        for(int i=2;i<=x/2;i++)
        {
            if (x % i == 0 )
                flag = 1;
        }
        if(x==1)
            out.println("1 is neither prime nor composite");
        if(flag==0 && x!=1)
            out.println("Given no:"+x+" is prime number");
        if(flag==1)
            out.println("The given number:"+x+" is not prime number");
    }
}

Output: 01

Java program to check prime number

Output: 02

Output: 03

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!