Created
April 7, 2022 13:34
-
-
Save aadipoddar/dad308293b465803a77a9447dfc44162 to your computer and use it in GitHub Desktop.
Calculate the factorial of that number using recursion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Write a program to accept a number | |
and calculate the factorial of that number using recursion. | |
INPUT: 5 | |
OUTPUT: 5 * 4 * 3 * 2 * 1 = 120 | |
*/ | |
import java.util.*; | |
class FactorialRecursion { | |
int factorial(int n) { | |
if (n == 0) | |
return 1; | |
else | |
return n * factorial(n - 1); | |
} | |
public static void main(String[] args) { | |
FactorialRecursion fr = new FactorialRecursion(); | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Enter a number"); | |
System.out.println("Factorial is: " + fr.factorial(sc.nextInt())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment