ืžื”ื™ ื’'ืื•ื•ื” ืจืงื•ืจืกื™ื”?

ื”ืชืจื—ื™ืฉ ืฉื‘ื• ืคื•ื ืงืฆื™ื” ืื• ืฉื™ื˜ื” ืžืชืงืฉืจืช ืœืขืฆืžื” ื™ื“ื•ืข ื‘ืชื•ืจ ืจืงื•ืจืกื™ื” ื•ืคื•ื ืงืฆื™ื” ืื• ืฉื™ื˜ื” ื›ื–ื• ื™ื“ื•ืขื” ื‘ืชื•ืจ ืคื•ื ืงืฆื™ื” ืจืงื•ืจืกื™ื‘ื™ืช ืื• ืฉื™ื˜ื” ืจืงื•ืจืกื™ื‘ื™ืช. ื’ื™ืฉื” ื–ื• ืžืฉืžืฉืช ืชืžื™ื“ ื›ื“ื™ ืœืคืจืง ื‘ืขื™ื•ืช ืžื•ืจื›ื‘ื•ืช ืœื‘ืขื™ื•ืช ืคืฉื•ื˜ื•ืช ื•ืงืœื•ืช ื™ื•ืชืจ ืœื›ืชื™ื‘ื”. ื“ื•ื’ืžื” ืคืฉื•ื˜ื” ื”ื™ื ืœื”ืฆื™ื‘ ืฉืชื™ ืžืจืื•ืช ืื—ืช ืžื•ืœ ื”ืฉื ื™ื™ื” ื•ื”ืŸ ื™ืชื—ื™ืœื• ืœืฉืงืฃ ืื—ืช ืืช ื”ืฉื ื™ื™ื” ื‘ืื•ืคืŸ ืจืงื•ืจืกื™ื‘ื™ ืฉื ื™ืชืŸ ืœืจืื•ืช ื‘ืงืœื•ืช.

ืจืงื•ืจืกื™ื” ืขืงื™ืคื”

ื”ื“ื•ื’ืžื” ืฉืœืžืขืœื” ืžื™ื™ืฆื’ืช ืืช ื”ืจืงื•ืจืกื™ื” ื”ืขืงื™ืคื” , ื›ืœื•ืžืจ, ืคื•ื ืงืฆื™ื” ืื—ืช ื”ืงื•ืจืืช ืœืคื•ื ืงืฆื™ื” ืฉืชื™ื™ื ื•ืคื•ื ืงืฆื™ื” ืฉืชื™ื™ื ืงื•ืจืืช ืœืคื•ื ืงืฆื™ื” ืื—ืช.

ืจืงื•ืจืกื™ื” ื™ืฉื™ืจื”

ื›ืืฉืจ ืฉื™ื˜ื” ืงื•ืจืืช ืœืขืฆืžื” ืฉื•ื‘ ื•ืฉื•ื‘, ื”ื™ื ื ืงืจืืช ืจืงื•ืจืกื™ื” ื™ืฉื™ืจื” .

ืžืฆื‘ ืขืฆื™ืจื”

ืขื›ืฉื™ื• ืืชื” ืชื—ืฉื•ื‘ ืื ืฉื™ื˜ื” ืงื•ืจืืช ืœืขืฆืžื” ืฉื•ื‘ ื•ืฉื•ื‘ ืื– ื‘ื˜ื•ื— ืฉื”ื™ื ืชืชืžื•ื“ื“ ืขื ืฉื’ื™ืืช Stack Overflow . ื›ืŸ, ืืชื” ืฆื•ื“ืง, ื›ืžื• ืœื•ืœืื” ืื™ื ืกื•ืคื™ืช ื–ื” ื™ื›ื•ืœ ืœื”ื™ื›ื ืก ืœืจืงื•ืจืกื™ื” ืื™ื ืกื•ืคื™ืช. ืขืœื™ื ื• ืœืกืคืง ืชื ืื™ ื‘ืกื™ืก ืื• ืชื ืื™ ืกื™ื•ื ืขืœ ืžื ืช ืœืขืฆื•ืจ ืืช ื”ืจืงื•ืจืกื™ื” ื”ืื™ื ืกื•ืคื™ืช ื”ื–ื•, ื”ื™ื“ื•ืขื” ื›ืชื ืื™ ืขืฆื™ืจื”, ื•ืชื ืื™ ื–ื” ืžืกื•ืคืง ืชืžื™ื“ ื‘ืชื•ืš ืฉื™ื˜ืช ื”ืจืงื•ืจืกื”, ืื•ืชื” ื ืจืื” ื‘ืคื™ืจื•ื˜ ื‘ื“ื•ื’ืžื” ืœืžื˜ื”.

ื“ื•ื’ืžื” ืœืจืงื•ืจืกื™ื”

class Recursion {

    static int factorial( int n ) {
        // base condition or termination condition
        if (n != 0)
        // here we are calling the recursive method
            return n * factorial(n-1);
        else
            return 1;
    }

    public static void main(String[] args) {
        // initializing the variables
        int number = 5, result;
        result = factorial(number);
        System.out.println(number + " factorial = " + result);

        // changing the value from 5 to 10
        number = 10;
        result = factorial(number); // recursive function call
        System.out.println(number + " factorial = " + result);
    }
}

ืคืœื˜ ืœื“ื•ื’ืžื”

5 ืคืงื˜ื•ืจื™ = 120 10 ืคืงื˜ื•ืจื™ = 3628800
ื‘ื“ื•ื’ืžื” ืฉืœ ืฉื™ื˜ืช ื”ืจืงื•ืจืกื” ื”ื–ื• ื™ืฉ ืœื ื• ืคื•ื ืงืฆื™ื” ืคืงื˜ื•ืจืืœื™ืช ืฉื ืงืจืืช ืžื”ืฉื™ื˜ื” ื”ืจืืฉื™ืช. ื”ืขื‘ืจื ื• ืขืจืš ืฉืœ 5 ื›ื“ื™ ืœืจืื•ืช ืžื” ื”ืคืงื˜ื•ืจืืœื™ ืฉืœื•, ืฉื”ื•ื 120. ื”ืคื•ื ืงืฆื™ื” ื”ืคืงื˜ื•ืจืืœื™ืช ื”ื–ื• ืงื•ืจืืช ืœืขืฆืžื” ืฉื•ื‘ ืขืœ ื™ื“ื™ ื”ืขื‘ืจืช ื”ืขืจืš ืฉืœ 4, ืฉืฉื•ื‘ ืงื•ืจืืช ื•ืžืขื‘ื™ืจื” ืืช ื”ืขืจืš ืฉืœ 3, ื•ื›ืŸ ื”ืœืื”, ืขื“ ืฉืชื ืื™ ื”ืขืฆื™ืจื” ื”ื•ื met, ืฉื”ื•ื ื”ืขืจืš ืฉืœ 0. ืื•ืชื• ื”ื“ื‘ืจ ืงื•ืจื” ื›ืืฉืจ ืฉื™ื ื™ื ื• ืืช ื”ืขืจืš ืž-5 ืœ-10.

ื™ืชืจื•ื ื•ืช ื•ื—ืกืจื•ื ื•ืช ืฉืœ ืจืงื•ืจืกื™ื”

ืงื•ื“ ืจืงื•ืจืกื™ื‘ื™ ื”ื•ื ืคืฉื•ื˜ ืœื›ืชื™ื‘ื”, ื ื™ืคื•ื™ ื‘ืื’ื™ื ื•ืชื—ื–ื•ืงื”, ื•ื–ื” ื—ื•ืกืš ื–ืžืŸ. ืื‘ืœ ืžืฆื“ ืฉื ื™, ื‘ื›ืœ ืงืจื™ืื” ื”ื–ื™ื›ืจื•ืŸ ื ืฆืจืš ืขื‘ื•ืจ ื”ืžืฉืชื ื™ื, ื•ื‘ื’ืœืœ ื–ื” ื™ื›ื•ืœ ืœื”ื™ื’ืžืจ ืœื ื• ื”ื–ื™ื›ืจื•ืŸ.

ืกื™ื›ื•ื

ืื ื• ืžืงื•ื•ื™ื ืฉืืชื” ืžื‘ื™ืŸ ืืช ื”ืจืงื•ืจืกื™ื” ืฉืœ Java ื•ื›ื™ืฆื“ ืœื™ื™ืฉื ืื•ืชื”, ื›ืžื• ื’ื ืืช ื”ื™ืชืจื•ื ื•ืช ื•ื”ื—ืกืจื•ื ื•ืช ืฉืœื”. ืืœ ืชื”ืกืก ืœื”ืชืืžืŸ ื•ืœื—ื–ื•ืจ ื‘ื›ืœ ืคืขื ืฉืชื–ื“ืงืง ืœืกื™ื•ืข ื ื•ืกืฃ. ืœืžื™ื“ื” ืžื”ื ื”!