Like Tree1Likes
  • 1 Post By Meerul264

Please explain to me how this code work so it make sense to me.

This is a discussion on Please explain to me how this code work so it make sense to me. within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; double factorial ( double a ) { if (a > 1 ) return ( ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77

    Please explain to me how this code work so it make sense to me.

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    
    
    double factorial ( double a )
    {
        if (a > 1 )
            return ( a * factorial (a -1) );
        else
            return (1);
    }
    
    
    int main()
    {
        double num;
        cout << "Enter a number: ";
        cin >> num;
    
    
        cout << num << "! = " << factorial(num);
    
    
        system("pause");
        return 0;
    }
    Now I know that the working for factorial is

    n! = n * ( n - 1) * ( n - 2) * ( n - 3) .... * 1

    but I can't see how this is implemented on the code.

    Please help me to understand the code. Thanks.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Well, you have to realize that:
    Code:
    n! = n* (n-1)!
    and that the factorual of 1 is always 1.
    After that look at the factorial function, it'll be clear.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,299
    You are also aware that n! = n*(n-1)! if n is positive, and that both 0! and 1! are equal to 1?

    So, if wanting to compute 4! one might calculate 4*3!. That requires calculating 3!, which is 3*2!. That requires calculating 2!, which is 2*1!. 1! is 1.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    So, if wanting to compute 4! one might calculate 4*3!. That requires calculating 3!, which is 3*2!. That requires calculating 2!, which is 2*1!. 1! is 1.
    I understand your point, but I still can't understand how the function can work it out like that.

    This code is shown under the topic function (II), recursivity.

    Maybe you can explain to me about recursivity briefly to explain how this work?

    Thanks again.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    Code:
    double factorial0 ( double a ) {
        return 1;
    }
    double factorial1 ( double a ) {
        return 1 * factorial0();
    }
    double factorial2 ( double a ) {
        return 2 * factorial1();
    }
    double factorial3 ( double a ) {
        return 3 * factorial2();
    }
    double factorial4 ( double a ) {
        return 4 * factorial3();
    }
    It's like this, but without all the hassle of copy/paste/rename.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    And how does

    Code:
    double factorial0 ( double a ) {
        return 1;
    }
    double factorial1 ( double a ) {
        return 1 * factorial0();
    }
    double factorial2 ( double a ) {
        return 2 * factorial1();
    }
    double factorial3 ( double a ) {
        return 3 * factorial2();
    }
    double factorial4 ( double a ) {
        return 4 * factorial3();
    }
    
    


    be similar with this?

    Code:
    double factorial ( double a )
    {
        if (a > 1 )
            return ( a * factorial (a -1) );
        else
            return (1);
    }
    
    


    Sorry, after all that, I still need further explanation to understand.




  7. #7
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    Ohh I get it now. So the work out s somewhat like..

    if i put a = 5,

    5 * ( factorial ( a -1 ))

    then

    5 * factorial ( 5 -1) same as a a* factorial ( a - 1)

    5 * factorial (4)

    then

    5 * ( 4 * factorial ( 3 ) )

    again [4 * factorial (3)] is same as [a*factorial(a-1)]

    so

    5 * 4 * ( 3 * factorial (2) )

    5 * 4 * 3 * ( factorial (2) )

    5* 4 * 3 * ( 2 * factorial ( 1 ) )

    5 * 4 * 3 * 2 * 1 = 120..

    Is my understanding correct?
    iMalc likes this.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    Yes, that's it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    Alright thanks all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 04-13-2010, 11:48 PM
  2. does it make any sense
    By ExDHaos in forum C++ Programming
    Replies: 0
    Last Post: 05-23-2009, 08:06 AM
  3. does this make sense ??
    By agarwaga in forum C++ Programming
    Replies: 14
    Last Post: 03-08-2006, 06:10 PM
  4. anyone make any sense of this
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 10-03-2001, 10:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21