Thread: Recursion

  1. #1
    Peaches
    Guest

    Lightbulb Recursion

    I'm not very good at recursion - how would I write a program to show compound interest of 1%/month over 12 months?
    I'm supposed to convert this factorial example:

    Code:
    #include<stdio.h>
    int factorial(long);
    
    int main(void)
    {
    int i;
    for (i=1; i<=10; i++)
        printf("%2d! = %ld\n", i, factorial(i));
    return (0);
    }
    
    int facotial(long num)
    {
    if (number <= 1)
        return (1);
    else
        return (number * factorial(number -1));
    }

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Do you know how to work out compound interest mathematically?

    translate that into pseudocode and all will become clearer.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Peaches
    Guest
    Aaahh, recursion is starting to become clearer!

    Code:
    float interest(float n, int a)
    {
        if (a<=1)
            return (n * 1.01);
        else
            return (1.01 * interest(n, a-1));
    }
    Many thanks!

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    The general rule is that if you can do something without recursion then you should use the non-recursive function. This can be done without recursion so I would change the code. But I'm a stickler for optimization.

  5. #5
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    but recursion is pretty

    and readable, another good thing. take readable recursion unless you're an optimization sticklet ;D
    .sect signature

  6. #6
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    use the real formula for compound interest
    hello, internet!

  7. #7
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    This is not a rebuttal to Master5001, it's just a response to this kind of quote, which I've seen posted numerous times over the years--

    The general rule is that if you can do something without recursion then you should use the non-recursive function.
    I was never much for following rules that were created by eggheads who don't really know how to program.

    Recursion is a perfectly valid programming method. The _only_ time you don't use it is if using it creates additional problems-- such as

    a) stack overflow
    b) performance decrease

    Do what MAKES SENSE, not what some zealot purist egghead thinks is right based on no understanding of reality.
    It is not the spoon that bends, it is you who bends around the spoon.

  8. #8
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    its all really irrelevant in this case, because the real compound interest forumula uses no loops or recursion.

    imo, a loop is usually easier to understand than recursion anyways, but use whatever fits the situation.
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. convert Recursion to linear can it be done
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2008, 02:58 AM
  3. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  4. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  5. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 09:24 AM