Skip to content

Instantly share code, notes, and snippets.

@aadipoddar
Created April 7, 2022 13:34
Show Gist options
  • Save aadipoddar/dad308293b465803a77a9447dfc44162 to your computer and use it in GitHub Desktop.
Save aadipoddar/dad308293b465803a77a9447dfc44162 to your computer and use it in GitHub Desktop.
Calculate the factorial of that number using recursion
/*
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