Tuesday 15 July 2014

java - Factorial of a number using recursion -


I have below the recursive function to calculate the number of factual numbers. When I remove the condition, the program works well. Why can anyone explain this?

This is the code that works fine -

  Public stable long-term actual user (int number) {if (number == 1) {return 1; } Other {Return no. * Factualizing Riction (number -1); }}   

without condition (code which throws error),

  public static long-term factorial protector (int no) {return number * fact User Protector (number - 1); }   

I get stack overflow error

Thread "main" java.lang.StackOverflowError at birst. Exception in FactorialUsingRecursion.factUsingRecursion (FactorialUsingRecursion.java:10)

Why request experts advise me why this is the case?

This loses one of those things which makes the recursive function recursive in which no exit The situation is not there.

All recurring solutions should meet three rules or properties: A recursive solution should have a base case. A recursive solution should have a recursive case.

From: Python using Data Structure and Algorithms

No comments:

Post a Comment