Back
Calculate factorial using recursion in JAVA.
class FindFactorial{ public static void main(String args[]){ int factorial = fact(5); System.out.println("Factorial of 5 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; }}
class FindFactorial{
public static void main(String args[]){
int factorial = fact(5);
System.out.println("Factorial of 5 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;
Output:
Factorial of 5 is: 120
31k questions
32.8k answers
501 comments
693 users