ืืื ื'ืืืื ืจืงืืจืกืื?
ืืชืจืืืฉ ืฉืื ืคืื ืงืฆืื ืื ืฉืืื ืืชืงืฉืจืช ืืขืฆืื ืืืืข ืืชืืจ ืจืงืืจืกืื ืืคืื ืงืฆืื ืื ืฉืืื ืืื ืืืืขื ืืชืืจ ืคืื ืงืฆืื ืจืงืืจืกืืืืช ืื ืฉืืื ืจืงืืจืกืืืืช. ืืืฉื ืื ืืฉืืฉืช ืชืืื ืืื ืืคืจืง ืืขืืืช ืืืจืืืืช ืืืขืืืช ืคืฉืืืืช ืืงืืืช ืืืชืจ ืืืชืืื. ืืืืื ืคืฉืืื ืืื ืืืฆืื ืฉืชื ืืจืืืช ืืืช ืืื ืืฉื ืืื ืืื ืืชืืืื ืืฉืงืฃ ืืืช ืืช ืืฉื ืืื ืืืืคื ืจืงืืจืกืืื ืฉื ืืชื ืืจืืืช ืืงืืืช.ืจืงืืจืกืื ืขืงืืคื
ืืืืืื ืฉืืืขืื ืืืืฆืืช ืืช ืืจืงืืจืกืื ืืขืงืืคื , ืืืืืจ, ืคืื ืงืฆืื ืืืช ืืงืืจืืช ืืคืื ืงืฆืื ืฉืชืืื ืืคืื ืงืฆืื ืฉืชืืื ืงืืจืืช ืืคืื ืงืฆืื ืืืช.ืจืงืืจืกืื ืืฉืืจื
ืืืฉืจ ืฉืืื ืงืืจืืช ืืขืฆืื ืฉืื ืืฉืื, ืืื ื ืงืจืืช ืจืงืืจืกืื ืืฉืืจื .ืืฆื ืขืฆืืจื
ืขืืฉืื ืืชื ืชืืฉืื ืื ืฉืืื ืงืืจืืช ืืขืฆืื ืฉืื ืืฉืื ืื ืืืื ืฉืืื ืชืชืืืื ืขื ืฉืืืืช 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.
GO TO FULL VERSION