Skip to content

Commit 2dbd83c

Browse files
authored
Merge pull request #95 from Nehansh10/dev
Last Digit of Fibonnaci
2 parents a0a0d34 + acfbaad commit 2dbd83c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Last Digit of FIbonnaci.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.*;
2+
3+
public class FibonacciLastDigit {
4+
5+
private static int getFibonacciLastDigitNaive(int n) {
6+
if (n <= 1)
7+
return n;
8+
9+
int previous = 0;
10+
int current = 1;
11+
12+
for (int i = 0; i < n - 1; ++i) {
13+
int tmp_previous = previous;
14+
previous = current;
15+
current = (tmp_previous + current)%10;
16+
}
17+
18+
return current;
19+
}
20+
21+
public static void main(String[] args) {
22+
Scanner scanner = new Scanner(System.in);
23+
int n = scanner.nextInt();
24+
int c = getFibonacciLastDigitNaive(n);
25+
System.out.println(c);
26+
}
27+
28+
}

0 commit comments

Comments
 (0)