Advertisement
Georgi_Benchev

Untitled

Dec 5th, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.58 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Task3_Fibonacci {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         int n = Integer.parseInt(scanner.nextLine());
  8.         System.out.println(Fibonacci(n, 0, 1));
  9.  
  10.  
  11.     }
  12.  
  13.     public static long Fibonacci(int n, long num1, long num2) {
  14.         if (n == 0) {
  15.             return 0;
  16.         } else if (n == 1) {
  17.             return 1;
  18.         } else if (n == 2) {
  19.             return num1 + num2;
  20.         } else {
  21.             return Fibonacci(n - 1, num2, num1 + num2);
  22.         }
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement