Click here to Skip to main content
15,920,633 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Can anyone help me i need to make a method that will take an argument as an integer and then try to calculate it but it shows an stack Overflow Exception so if the value passes the capacity of the integer it should calculate the number in the long type with recursion method
Posted
Comments
PIEBALDconsult 6-May-13 21:05pm    
Yeah, recursion is the wrong solution -- unless this is homework, in which case you have to do it yourself.

 
Share this answer
 
Comments
PokerFace12 6-May-13 19:01pm    
I halve tried this but i don't know how to construct the method with the exception handler
PokerFace12 6-May-13 19:05pm    
i have this methods :
public long Factorial_Recursion_Long(ref int n)
{
if (n <= 1)
return 1;
return checked( n * Factorial_Recursion_Long(n - 1));

}
public int Factorial_Recursion_int( ref int n)
{
if (n <= 1)
return 1;
return checked( n * Factorial_Recursion_int(n - 1));

}
Sergey Alexandrovich Kryukov 6-May-13 19:51pm    
No, OP's problem is stack overflow; so BigInteger would not help. This is "infinite" recursion. Of course, BigInteger is a great thing, but it's even more important to use recursion properly.
For factorial, recursion is really a bad idea, iterative solution should be used.
—SA
Bad idea. You should use iterative solution, based on loops. I live the implementation for your home exercise. This is really easy. Recursion is a great thing, but you should think: what to use for what.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900