Advertisement
redo21

fibonacci.java

Aug 30th, 2022
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.48 KB | None | 0 0
  1. public static int fibonacci(int n){
  2.         int Un = 0;
  3.         int Un1 = 1;
  4.         int Un2 = 1;
  5.         int temp = 0;
  6.  
  7.         for(int i = 0; i <= n; i++) {
  8.             if(i == 0) {
  9.                 temp = 0;
  10.             }
  11.  
  12.             if(i == 1) {
  13.                 temp = 1;
  14.             }
  15.  
  16.             else {
  17.                 Un2 = Un1;
  18.                 Un1 = Un;
  19.                 Un = Un2 + Un1;
  20.                 temp = Un;
  21.             }
  22.         }
  23.         return temp;
  24.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement