-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNth Fibonacci number
73 lines (44 loc) · 1.08 KB
/
Nth Fibonacci number
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
62
63
64
65
66
67
68
69
70
71
72
73
Nth term of fibonacci series F(n) is calculated using following formula -
Sample Input 1:
4
Sample Output 2:
3
Sample Input 1:
6
Sample Output 2:
8
JAVA CODE:-
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Your class should be named Solution.
* Read input as specified in the question.
* Print output as specified in the question.
*/
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
// int n1=0,n2=1,n3=0;
// int[] arr=new int[25];
// arr[0]=n1;
// arr[1]=n2;
// for(int i=2;i<=25;i++)
// {
// n3=n1+n2;
// arr[i]=n3;
// n1=n2;
// n2=n3;
// }
// for(int i=0;i<=arr[n];i++)
// {
// System.out.println(arr[n]);
// }
int a[]=new int[n+2];
a[0]=0;
a[1]=1;
for(int i=2;i<=n;i++)
{
a[i]=a[i-1]+a[i-2];
}
System.out.print(a[n]);
}
}