Thread: slight calculation error with my compound interest calculator

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    5

    slight calculation error with my compound interest calculator

    Hey guys, I seem to have a slight calculation error and I need to know if it is an issue with the compiler or a mistake that I made.

    With an initial investment of $1000 gaining 8% interest for 10 years the final output I get is $2158.93 ($2158.925049). However, using my TI-84 calculator I get $2158.92 ($2158.924997).

    Is there a way to fix these little errors? I have noticed miscalculations like this in a few other programs, of course all of which have been very small like the error above.

    Looking forward to the advice! Thank you!

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        float a, r, x; int n; // variables
        // asking for the initial deposit
        printf("What is the initial deposit?"); fflush(stdout);
        scanf("%f", &x);
        // asking for the length of time
        printf("For how many years will your money be invested?"); fflush(stdout);
        scanf("%d", &n);
        // asking for the annual rate of return
        printf("What is your expected annual rate of return?"); fflush(stdout);
        scanf("%f", &r);
        //calculating the total amount after n years
        a = x * pow(1 + r/100,n);
        printf("After %d years your initial investment of $%.2f will be worth $%.2f.", n, x, a);
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2015
    Posts
    65
    Try to use double instead of float. If I'm not much mistakes, it should make a significant difference when it comes to precision, but also make sure to consider how much precision you actually need.

    Also, in pow() you should probably cast 'r' to a float, or as mentioned double, to ensure no integer division takes place, in case r is a whole number.

    Otherwise it all seem okay to me.

    edit:
    This gives me the same result as your TI-84. The cast is apparently not necessary, but better to be on the safe side, so I've added a .0 to the 100. And it was double vs float that made all the difference.
    Code:
    #include <stdio.h>#include <math.h>
    
    
    int main()
    {
        double a, r, x;
        int n;
        x = 1000;
        n = 10;
        r = 8;
        a = x * pow(1 + r/100.0,n);
        printf("After %d years your initial investment of $%.2f will be worth $%.6f.\n", n, x, a);
        return 0;
    }
    Last edited by Zacariaz; 05-29-2015 at 03:57 AM.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compound Interest?
    By jbone0881 in forum C Programming
    Replies: 6
    Last Post: 02-25-2011, 11:07 PM
  2. Replies: 2
    Last Post: 04-01-2009, 12:06 AM
  3. need help with compound interest
    By JoelearningC in forum C Programming
    Replies: 4
    Last Post: 03-09-2008, 10:32 AM
  4. compound interest, need help
    By shaitan_superma in forum C Programming
    Replies: 8
    Last Post: 09-01-2007, 11:50 PM
  5. compound interest
    By bliznags in forum C Programming
    Replies: 11
    Last Post: 03-20-2005, 01:46 AM