-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFibonacci.java
More file actions
38 lines (33 loc) · 1.38 KB
/
Copy pathFibonacci.java
File metadata and controls
38 lines (33 loc) · 1.38 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
package Fibonacci;
import java.util.Scanner; // Scanner class is required to get input from user
public class Fibonacci {
static int totalTerms = 0; // total number of terms to display
public static void main(String[] args) {
// if user provided commandline-arguments, then
if (args.length != 0) {
if (args.length == 1)
totalTerms = Integer.parseInt(args[0]);
else
System.out.println("[!] Available arguments: [Number of Terms]");
} // else get input from user
else {
Scanner scanner = new Scanner(System.in);
System.out.print("[<-] Enter the number of terms: ");
totalTerms = scanner.nextInt();
scanner.close();
}
System.out.printf("[->] Fibonacci Series upto %s terms:", totalTerms);
for (int i = 0; i < totalTerms; i++) {
if (i % 10 == 0)
System.out.println();
System.out.format("%10d", generateFibonacciRecursive(i));
}
}
// a recursive method to generate the n-th number from Fibonacci Series
public static int generateFibonacciRecursive(int currentTerm) {
if (currentTerm == 0 || currentTerm == 1)
return currentTerm;
else
return generateFibonacciRecursive(currentTerm - 1) + generateFibonacciRecursive(currentTerm - 2);
}
}