#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    C programming help


    Hello. I'm taking C programming in college. I'm using W7 and code::blocks IDE. This is a homework problem.

    The code works but I don't know why at the start of the printed code I have 1000.00 under Total Yearly Amount.

    This is my first post. could anyone help me with my code? I don't need the 1000.00 to show. It should start with 1 80.00 1080.00.

    I'm not sure what I did wrong.

    Thank you for viewing. I posted the code below. Thank you for looking.

    Josh

    Code:
    #include <stdio.h>
    #define PRINCIPAL 1000.00
    #define RATE 0.08
    #define MAXCOUNT 10
    
    int main()
    {
        int counter = 0;
        float interest, balance, totalYearly, totalInterest, interestEarned;
        printf("Your bank account starts with $1000.00\nand for 10 years 8%% interest is added to each year's total\n\n");
    
        printf("YEAR   INTEREST EARNED   TOTAL YEARLY AMOUNT\n");
        printf("----   ---------------   -------------------\n");
    
        while (counter < MAXCOUNT)
        {
            interest = PRINCIPAL * RATE;
    
            interestEarned = totalInterest + interest;
    
            balance = totalInterest + balance;
    
            totalYearly = PRINCIPAL + balance;
    
            totalInterest = totalYearly * RATE;
    
            counter ++;
    
           
            printf("%2d         %6.2f             %5.2f\n", counter, interestEarned, totalYearly);
        }
    
        return 0;
    }
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Note here

    Code:
            interest = PRINCIPAL * RATE;
    
            interestEarned = totalInterest + interest;
    
            balance = totalInterest + balance;
    
            totalYearly = PRINCIPAL + balance;
    
            totalInterest = totalYearly * RATE;
    totalInterest is not being set before you add it to the balance, so the balance is 0.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0
    Thank you for your reply. Are you saying that I have to reorder it?


    Code:
            totalInterest = totalYearly * RATE;
    
            interest = PRINCIPAL * RATE;
    
            interestEarned = totalInterest + interest;
    
            balance = totalInterest + balance;
    
            totalYearly = PRINCIPAL + balance;
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0
    I got it. I had to add the interest to the totalYearly and it all came out good. Thank you for the help.

    Josh

    Code:
    #include <stdio.h>
    #define PRINCIPAL 1000.00
    #define RATE 0.08
    #define MAXCOUNT 10
    
    int main()
    {
        int counter = 0;
        float interest, balance, totalYearly, totalInterest, interestEarned;
        printf("Your bank account starts with $1000.00\nand for 10 years 8%% interest is added to each year's total\n\n");
    
        printf("YEAR   INTEREST EARNED   TOTAL YEARLY AMOUNT\n");
        printf("----   ---------------   -------------------\n");
    
        while (counter < MAXCOUNT)
        {
            totalInterest = totalYearly * RATE;
    
            interest = PRINCIPAL * RATE;
    
            balance = totalInterest + balance;
    
            interestEarned = totalInterest + interest;
    
            totalYearly = PRINCIPAL + balance + interest;
    
    
    
    
    
    
            counter ++;
    
    
            printf("%2d         %6.2f             %5.2f\n", counter, interestEarned, totalYearly);
        }
    
        return 0;
    }

IMN logo majestic logo threadwatch logo seochat tools logo