You must Sign In to post a response.
  • Category: .NET

    Issue with the factorial program

    Hai Guys,
    Below is a questions asked in one of the written test:

    int factorial(int n)
    if(n==0) return 1;
    return n*factorial(n);

    They asked that "what are all the problems in this function"?
    As we can see the statement leads to the infinite loop so memory issue will come.
    The actual function should be:

    int factorial(int n)
    if(n==0) return 1;
    return n*factorial(n-1);

    This will fix the issue and will give the correct result.
    The answer was also given similar as below:
    Problems in the Program:
    1. Method name should be in Camel code: e.g. Factorial
    2. The function returns n*Factorial(n) which will make the programs runs forever as it is looping itself.
    3. StackOverflow exception will occur when we run the program.
    4. The correction should be as below:
    int Factorial(int n)
    if (n == 0)
    return 1;
    return n*Factorial(n - 1);
    But they have reviewed it and responded like:
    =>For the factorial question, the result is too basic.
    What is it mean? Can someone give some other idea that what they could have expected on this?
  • #763159
    Following is the tutorial that shows how memory is used when every variables are used and loops are used also it strings points in details with memory allocation. written in C#.


    Delete Attachment

  • Sign In to post your comments