Skip to content

Commit

Permalink
Added a factorial file using recursion in java
Browse files Browse the repository at this point in the history
  • Loading branch information
vipulSVJ committed Oct 20, 2023
1 parent e918b31 commit 9965d3a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions recursiveFactorial.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.Scanner;

public class recursiveFactorial {

public static void main(String args[]) {
// Scanner object for capturing the user input
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number:");
// Stored the entered value in variable
int num = scanner.nextInt();
// Called the user defined function fact
int factorial = fact(num);
System.out.println("Factorial of entered number is: " + factorial);
}

static int fact(int n) {
int output;
if (n == 1) {
return 1;
}
// Recursion: Function calling itself!!
output = fact(n - 1) * n;
return output;
}
}

0 comments on commit 9965d3a

Please sign in to comment.