0

I am in 11th grade and I am learning C#. While I tried to do a factorial recursion function that divides 1 by the factorial number. When I run it it gives me the error of "Unhandled exception. System.DivideByZeroException: Attempted to divide by zero." I didn't divide anything by zero... If you can help me and explain the problem it will be very helpful, thanks a lot.

static int FactorialRecursion(int num)
{ 
    if (num == 1)
        {
            return 1;
        }
    return 1 / (num * FactorialRecursion(num - 1));
}
  • 1
    Welcome to programming and SO! I've marked a duplicate question., but the cause is that integer division returns an integer, so `1/2 = 0` and not `0.5`, – D Stanley Oct 22 '21 at 17:45
  • But why are you dividing by 1 in the first place? A factorial could be done with just integer multiplication. – D Stanley Oct 22 '21 at 18:04

0 Answers0