Advertisement
Spocoman

03. Recursive Fibonacci

Oct 27th, 2024
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.40 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class Main {
  4.     public static int getFibonacci(int n) {
  5.         if (n == 1 || n == 2) {
  6.             return 1;
  7.         }
  8.         return getFibonacci(n - 1) + getFibonacci(n - 2);
  9.     }
  10.    
  11.     public static void main(String[] args) {
  12.         Scanner scanner = new Scanner(System.in);
  13.         int n = Integer.parseInt(scanner.nextLine());
  14.        
  15.         System.out.print(getFibonacci(n));
  16.     }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement