Skip to content

Commit 9965d3a

Browse files
committed
Added a factorial file using recursion in java
1 parent e918b31 commit 9965d3a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

recursiveFactorial.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.Scanner;
2+
3+
public class recursiveFactorial {
4+
5+
public static void main(String args[]) {
6+
// Scanner object for capturing the user input
7+
Scanner scanner = new Scanner(System.in);
8+
System.out.println("Enter the number:");
9+
// Stored the entered value in variable
10+
int num = scanner.nextInt();
11+
// Called the user defined function fact
12+
int factorial = fact(num);
13+
System.out.println("Factorial of entered number is: " + factorial);
14+
}
15+
16+
static int fact(int n) {
17+
int output;
18+
if (n == 1) {
19+
return 1;
20+
}
21+
// Recursion: Function calling itself!!
22+
output = fact(n - 1) * n;
23+
return output;
24+
}
25+
}

0 commit comments

Comments
 (0)