Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* числа Фибоначчи и факториал через рекурсию*/
- package com.company;
- public class Main {
- public static void main(String[] args) {
- for (int i = 0; i < 10; ++i) System.out.printf("%d!=%d\n",i,factorial(i));
- for (int i = 0; i < 10; ++i) System.out.printf("Fibonachi № %d=%d\n",i,fibonachi(i));
- }
- public static int factorial ( int n)
- {
- if (n < 0) throw new IllegalArgumentException();
- return ((n==0)||(n==1)) ? 1 : n * factorial(n-1);
- }
- public static int fibonachi(int n)
- {
- if (n < 0) throw new IllegalArgumentException();
- return ((n==0)||(n==1)) ? n : fibonachi(n-1) + fibonachi(n-2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement