Created
November 19, 2015 09:16
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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