Write a java program to check prime number.

import java.util.Scanner;
class PrimeCheck
{
   public static void main(String args[])
   {  
 int temp;
 boolean isPrime=true;
 Scanner scan= new Scanner(System.in);
 System.out.println("Enter any number:");
 //capture the input in an integer
 int num=scan.nextInt();
        scan.close();
 for(int i=2;i<=num/2;i++)
 {
           temp=num%i;
    if(temp==0)
    {
       isPrime=false;
       break;
    }
 }
 //If isPrime is true then the number is prime else not
 if(isPrime)
    System.out.println(num + " is a Prime Number");
 else
    System.out.println(num + " is not a Prime Number");
   }
}

Popular posts from this blog

Priority Scheduling Algorithm Java Program.

Implement UNIX system calls like ps, fork, join, exec family, and wait for process management (use shell script/ Java/ C programming).

Implement a class CppArray which is identical to a one-dimensional C++ array (i.e., the index set is a set of consecutive integers starting at 0) except for the following : 1. It performs range checking. 2.It allows one to be assigned to another array through the use of assignment operator. 3.It supports a function that returns the size of the array.