-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNthFibbonacciNumber.java
More file actions
38 lines (32 loc) · 1.17 KB
/
NthFibbonacciNumber.java
File metadata and controls
38 lines (32 loc) · 1.17 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
// Nth Fibonacci Number
// Send Feedback
// The n-th term of Fibonacci series F(n), where F(n) is a function, is calculated using the following formula -
// F(n) = F(n - 1) + F(n - 2),
// Where, F(1) = 1, F(2) = 1
// Provided 'n' you have to find out the n-th Fibonacci Number. Handle edges cases like when 'n' = 1 or 'n' = 2 by using conditionals like if else and return what's expected.
// Example :
// Input: 6
// Output: 8
// Explanation: The number is ‘6’ so we have to find the “6th” Fibonacci number.
// So by using the given formula of the Fibonacci series, we get the series:
// [ 1, 1, 2, 3, 5, 8, 13, 21]
// So the “6th” element is “8” hence we get the output.
import java.util.Scanner;
public class NthFibbonacciNumber {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int f1 = 1, f2 = 1, fn = 0;
if (n == 1 || n == 2) {
fn = 1;
} else {
for (int i = 3; i <= n; i++) {
fn = f1 + f2;
f1 = f2;
f2 = fn;
}
}
System.out.println(fn);
s.close();
}
}