1. ## 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. Do you know how to work out compound interest mathematically?

translate that into pseudocode and all will become clearer.

3. 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. 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. but recursion is pretty

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

6. use the real formula for compound interest

7. 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.

8. 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.

Popular pages Recent additions