-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacciRecursion.java
More file actions
37 lines (29 loc) · 1 KB
/
FibonacciRecursion.java
File metadata and controls
37 lines (29 loc) · 1 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
// DONE
import java.util.Scanner; // input
public class FibonacciRecursion
{
static void process(int a, int b, int next, int n, int i)
{
// n is number and a is starting value which is one
// 0 1 1 2 3 5 (need to output this)
if (i < n) // 0 to n - 1
{ // as i is starting from 0
System.out.print(a + " "); // outputting
next = a + b;
a = b; // a now becomes end
b = next; // next gives new ending value
i++; // i = i + 1
process(a, b, next, n, i); // recursive procedure
}
}
public static void main(String[] args)
{
Scanner obj = new Scanner(System.in); // for amount of numbers to show
// PROMPT
System.out.print(" Enter how many terms to show : ");
int n = obj.nextInt();
// process without using return
process(0, 1, 0, n ,0); // (start, end, next, number, i)
obj.close(); // closing scanner object as this is end now
}
}