Write a program in Java to check prime numbers.

Write a program in Java to check prime numbers.

How the prime number is defined?

A prime number is a positive integer that is divisible only by 1 and itself. Any number that does not satisfy this condition is called a Non-prime number.

example for prime:- 2, 3, 5, 7, 11, 13, 17.

Video tutorial for Program to check Prime numbers

Video tutorial for Program to check Prime numbers

Program to check Prime numbers

import java.util.*;           //importing Scanner class
public class vtuupdates
{
    public static void main (String args[])
    {
        int flag=0;
    
        Scanner sc=new Scanner(System.in);           // Creat a Scanner Class

        System.out.print("Enter the number to Check Prime or Not Prime : ");
    
        int a=sc.nextInt();          // input number

       if(a==1 || a==0){
        flag=1;
       }
       else{

       for(int i=2; i<=a/2; i++){
        if(a%i==0){
            flag=1;
            break;
        }

     } 
}

     if(flag == 1){
        System.out.println(a+" is Not Prime");
     }
     else{
        System.out.println(a+ " is a Prime");
     }

    }
    }

Leave a Reply

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