Skip to content

Instantly share code, notes, and snippets.

@eMahtab
Created November 19, 2015 09:16
Show Gist options
  • Save eMahtab/6bc80b9cec5208b22bb7 to your computer and use it in GitHub Desktop.
Save eMahtab/6bc80b9cec5208b22bb7 to your computer and use it in GitHub Desktop.
Java program to check whether a number is Prime or not. Although not a very optimal way
import java.util.Scanner;
import java.util.InputMismatchException;
public class PrimeCheck1{
public static void main(String args[]){
System.out.print("Enter a number : ");
Scanner sc=new Scanner(System.in);
long number=-999;
long copyOfNumber=number;
try{
number=sc.nextLong();
copyOfNumber=number;
}catch(InputMismatchException ime){
System.err.println("Error : Please enter a valid number");
}
boolean prime=true;
for(long i=2;i<number;i++){
if((number % i)==0){
prime=false;
}
}
if(prime){
System.out.println("Number "+copyOfNumber+" is Prime ");
}else{
System.out.println("Number "+copyOfNumber+" is not Prime ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment