-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibo.java
61 lines (54 loc) · 1.4 KB
/
fibo.java
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
51
52
53
54
55
56
57
58
59
60
61
/**
* fibo.java
* @author: Herb Wolfe Jr <[email protected]>
*
* Simple program to display a sequence of Fibonnaci numbers.
*
* Notes: The 46th number is the largest in the sequence that can be stored
* in an int. The 92nd number is the largest that can be stored in a long
*/
import static java.lang.System.*;
import java.util.*;
public class fibo {
public static void main(String[] argv) throws Exception {
long f1, f2, f3;
int count = 0, max = 92;
boolean badVal;
String tmp;
if (argv.length == 0) {
badVal = true;
} else {
badVal = false;
try {
count = Integer.parseInt(argv[0]);
if ((count > max) || (count < 3)) {
badVal = true;
}
} catch (NumberFormatException ex) {
// parameter isn't an integer
badVal = true;
}
}
while (badVal) {
out.print("Enter how many Fibonacci numbers to print: ");
Scanner in = new Scanner(System.in);
tmp = in.next();
count = Integer.parseInt(tmp);
if ((count > max) || (count < 3)) {
out.println("Try a number between 3 and " + max);
} else {
badVal = false;
}
} // end while (badVal)
f1 = f2 = 1;
out.println("Fibonacci #1 is: " + f1);
out.println("Fibonacci #2 is: " + f2);
for (int i = 3; i <= count; i++) {
f3 = f1+f2;
out.println("Fibonacci #" + i + " is: " + f3);
f1 = f2;
f2 = f3;
}
out.println();
}
}