0

I have got the following program to compare two accounts, one with simple interest and the other with compound interest and I am sure that my result of 2.147 billion years is wrong, so could someone point out an error to me?

#include <stdio.h>

int main(void) {
    int simp_acct = 100, comp_acct = 100, years;
    simp_acct += 10; /*simple interest*/
    comp_acct *= .05; /*compound interest*/
    while(comp_acct < simp_acct) {
        simp_acct += 10; 
        comp_acct *= .05; 
        years++;
    }

    printf("Compound interest at .05%% beats simple interest at 10%% ");
    printf("after %d years.\n", years);
    return 0;
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
user3813418
  • 89
  • 1
  • 7
  • 5
    `years` is not initialized. Set it to zero. Interestingly, the result you got is [int.max](http://stackoverflow.com/questions/94591/what-is-the-maximum-value-for-a-int32). – Robert Harvey Jul 07 '14 at 17:38
  • Still got the same 2.147 billion area, though slightly more this time – user3813418 Jul 07 '14 at 17:41
  • 2
    Well, you're multiplying an `int` by a floating point number at `comp_acct *= .05;` and trying to store it back into an `int`. That probably doesn't make much sense. – Robert Harvey Jul 07 '14 at 17:43
  • 1
    Consider what happens when you do `comp_acct *= .05`. It's an equivalent of `comp_acct = comp_acct * .05`. What's the value of `comp_acct * .05`? –  Jul 07 '14 at 17:44
  • You changed the wrong one to `float`. – Barmar Jul 07 '14 at 17:46
  • 2
    Please don't edit the question to remove the error that we're helping you fix. Future readers need to see the problem when they read the question, it's not all about you. – Barmar Jul 07 '14 at 17:46
  • Still the same big old number when I make it `comp_acct += (comp_acct * .05)` – user3813418 Jul 07 '14 at 17:48
  • of course it will be the int.max value as you do only divide `comp_acct` and increment `simp_acct` and compare them, so this is just an infinite loop until the int.max is reached – Burak Jul 07 '14 at 17:50

2 Answers2

2

Change your variables to float:

float simp_acct = 100, comp_acct = 100, years;

Otherwise, your values will be rounded off to an integer when you multiply by a fraction.

You also need to fix the compound interest formula, it should be:

comp_acct *= 1.05;

You're just setting comp_acct to the interest, and losing the principle.

Barmar
  • 741,623
  • 53
  • 500
  • 612
2
#include <stdio.h>

int main() {
    double simp_acct = 100, comp_acct = 100;
    int years = 0;
    simp_acct += 10; /*simple interest*/
    comp_acct *= 1.05; /*compound interest*/
    while(comp_acct < simp_acct) {
        simp_acct += 10;
        comp_acct *= 1.05;
        years++;
    }

    printf("Compound interest at .05%% beats simple interest at 10%% ");
    printf("after %d years.\n", years);
    return 0;
}

Here is the working code. First of all if you keep multiplying by 0.05 you actually decrease the number and adding 10 increases it. What happens is simple_acct just overflows and the condition is met, because comp_acct becomes zero at some point. Second of all you use int and that won't give you true results when you work with floating point numbers.

hahcho
  • 1,369
  • 9
  • 17