Recursion

Exercise: Factorial Recursively

public class Main {
    public static int factorial(int n){
        if (n==0 || n==1)
            return 1;
        //also check
        if (n<0)
            return -1;

        return n*factorial(n-1);
    }

    public static void main(String[] args) {
        int n = 5;
        int res = factorial(n);
        System.out.println(res);
    }
}
120
What is the writing program for a factorial using recursion with the main function used only? - Quora

or in one line

Exercise - Reverse Recursively

Exercise: Print all permutations Recursively

permutation-Recursion

Exercise: Fibonacci

Fn=Fn1+Fn2F_{n}=F_{n-1}+F_{n-2}

and

F0=0,F1=1F_{0}=0,\quad F_{1}=1

sequence:

1,  1,  2,  3,  5,  8,  13,  21,  34,  55,  89,  144,  1,\;1,\;2,\;3,\;5,\;8,\;13,\;21,\;34,\;55,\;89,\;144,\;\ldots

another IO

Program to generate recursion tree for generic recursive program - Stack Overflow
1,  1,  2,  3,  5,  8,  13,  21,  34,  55,  89,  144,  1,\;1,\;2,\;3,\;5,\;8,\;13,\;21,\;34,\;55,\;89,\;144,\;\ldots

Exercise: Reduce

Reduec-java

Exercise: All subarrays

Exercise: Sudoku 2x2

Hamilton Institute Schools Maths Challenge

Source

code only

Last updated

Was this helpful?