-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsPrime.java
More file actions
50 lines (42 loc) · 1.03 KB
/
IsPrime.java
File metadata and controls
50 lines (42 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* IsPrime.java
* @author Herb Wolfe, Jr <hwolfe71@gmail.com>
*
* Created: 01/03/2014
*
* This program displays whether or not a number is prime.
* The number can either be passed as a command line parameter, or
* interactively.
*
*/
import static java.lang.System.*;
import java.util.*;
import java.io.*;
public class IsPrime {
/**
* Interactive method to display whether or not a number is prime.
*/
public static void main(String [] args) {
Scanner in = new Scanner(System.in);
Prime primes = new Prime();
long num;
String tmp;
if (args.length == 0) {
out.print("Enter the number to determine its primeness: ");
tmp = in.next();
} else {
tmp = args[0];
}
try {
num = Long.parseLong(tmp);
if (primes.isPrime(num)) {
out.println(num + " is a prime number.");
} else {
out.println(num + " is not a prime number.");
}
} catch (NumberFormatException ex) {
out.println("Invalid entry, goodbye!");
}
return;
}
}