Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (2.6k points)
I am getting error when I'm writing code for factorial using recursion in java?

Can somebody give me the solutions for this?

1 Answer

0 votes
by (7.2k points)

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;

   }

}

Output:

 

Factorial of 5 is: 120

Related questions

0 votes
1 answer
0 votes
1 answer
asked May 7, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer
asked Oct 31, 2020 by blackindya (18.4k points)

Browse Categories

...