-2

I just wrote this code :

#include <iostream>

using namespace std;

unsigned long long int choose(unsigned long long int k, unsigned long long int n)
{
    if (k==n or k==0)
    {
        return 1;
    }
    else
    {
        return (choose(n-1,k-1)+choose(n-1,k));
    }
}

int main(){
    cout << choose(3, 6);
}

but I got Run-Time Error, I think my problem is in my variables, I did debugging but I couldn't find the answer, why I got run time error?

Jarod42
  • 203,559
  • 14
  • 181
  • 302

1 Answers1

2

Since your variables are unsigned, substracting 1 from them when they're already 0 will make them roll over to the max value.

When n reaches 0 and you then call choose(n-1,k), this'll happen and that's the source of the issue (stackoverflow).

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625