October 19th, 2013, 08:21 AM
-
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;
}
October 19th, 2013, 08:56 AM
-
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.
October 19th, 2013, 09:05 AM
-
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;
October 19th, 2013, 09:44 AM
-
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;
}