Introduction
The below-given program lists the factorial of 1 to 20 and is a good example of Recursive Function in java:
Code
- public class RecursiveFunction {
- public static void main(String[] args) {
- System.out.println("Number\t\tFactorial");
- for (int i = 0; i <= 20; i++) {
- System.out.println(i + "\t\t" + Factorial(i));
- }
- }
- public static long Factorial(long x) {
- if (x == 0) {
- return 1;
- } else {
- return Factorial(x - 1) * x;
- }
- }
- }
Output
Join the conversation! Your thoughts help the community grow.