Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   C (https://www.askmehelpdesk.com/forumdisplay.php?f=438)
-   -   Recusion function (https://www.askmehelpdesk.com/showthread.php?t=269967)

  • Oct 16, 2008, 07:14 AM
    prashant kumar
    Recusion function
    Write the recursion function to print the factoial of a number
    And also give its working(dry run):)
  • Oct 16, 2008, 09:59 AM
    mani_jeddah

    although its your assignment but I will make it for you!

    #include<iostream>
    using namespace std;
    int fact(int,int);
    int main()
    {
    int ans ;
    int a ;
    cout << "Enter a number: "
    cin >> a ;
    ans = fact(a , 1);
    cout << ans ;
    return 0 ;
    }


    int fact(int b, int ans)
    {

    if(b == 0)
    return 1 ;
    else if(b == 1)
    return ans ;
    else
    ans *= b;
    return fact(b - 1, ans);
    }


    vote for my answer...

  • All times are GMT -7. The time now is 04:36 AM.