Advertisement
mrAnderson33

Четвертая лаба

Nov 21st, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1. /* числа Фибоначчи и факториал через рекурсию*/
  2.  
  3. package com.company;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         for (int i = 0; i < 10; ++i) System.out.printf("%d!=%d\n",i,factorial(i));
  10.         for (int i = 0; i < 10; ++i) System.out.printf("Fibonachi № %d=%d\n",i,fibonachi(i));
  11.     }
  12.  
  13.     public  static int  factorial ( int n)
  14.     {
  15.         if (n < 0) throw new IllegalArgumentException();
  16.         return ((n==0)||(n==1)) ? 1 : n * factorial(n-1);
  17.     }
  18.     public static int fibonachi(int n)
  19.     {
  20.         if (n < 0) throw new IllegalArgumentException();
  21.         return  ((n==0)||(n==1)) ? n  : fibonachi(n-1) + fibonachi(n-2);
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement