From 9965d3adcee06537f7d56adb9a51e293c96f1e87 Mon Sep 17 00:00:00 2001 From: Vipul Varshney Date: Fri, 20 Oct 2023 16:58:46 +0530 Subject: [PATCH] Added a factorial file using recursion in java --- recursiveFactorial.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 recursiveFactorial.java diff --git a/recursiveFactorial.java b/recursiveFactorial.java new file mode 100644 index 00000000..b0ea7693 --- /dev/null +++ b/recursiveFactorial.java @@ -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; + } +}