Skip to content

Instantly share code, notes, and snippets.

@nichtemna
Created August 10, 2016 16:11
Show Gist options
  • Select an option

  • Save nichtemna/f53386a8480829cb5913a9e8e84792f3 to your computer and use it in GitHub Desktop.

Select an option

Save nichtemna/f53386a8480829cb5913a9e8e84792f3 to your computer and use it in GitHub Desktop.
Last Digit of a Large Fibonacci Number
*/
public class FibLast {
public static void main(String[] args) {
Scanner scaner = new Scanner(System.in);
int n = scaner.nextInt();
System.out.println("Last digit for " + n + " = " + getFibonacci(n));
}
private static int getFibonacci(int n) {
int[] fib = new int[n+1];
for (int i = 0; i < fib.length; i++) {
if (i <= 1) {
fib[i] = i;
} else {
fib[i] = (fib[i-1]+ fib[i-2]) % 10;
}
}
for (int i : fib) {
System.out.println(i);
}
return fib[n];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment