I am having a bit of trouble with an assignment for my C programming class; I seem to be stuck on how to correctly implement & terminate a loop within the program.
This is my first time asking a question on StackOverflow, so I beg for your patience as I am a programming noob; I've been kind of afraid of posting here so as not to seem like an idiot. :P
Here are the instructions for the assignment:
"Write a program that calculates how many years it takes to accumulate a given amount of money to retire.
The program must prompt for: - starting balance - amount deposited every year - estimated annual interest rate earned (%) - target balance
Program outputs a table that shows information for each year. Calculate each entry in the table exactly as described here:
- year number
- amount deposited that year
- interest earned that year. Calculate as (balance at start of year + deposit) * rate
- balance at end of year. Is balance at start of year + deposit + interest
The loop must stop when the target is exactly reached or exceeded, and then a summary line is displayed. User input, the first few lines of output and example of the summary line are given below:
Enter starting balance ($): 250000.0
Enter amount deposited every year ($): 20000.0
Enter estimated annual interest rate (%): 10.0
Enter target balance ($): 2000000.0
Year Deposit Interest Balance
---- ------- -------- -------
0 250000.00 0.00 250000.00
1 20000.00 27000.00 297000.00
2 20000.00 31700.00 348700.00
3 20000.00 36870.00 405570.00
4 20000.00 42557.00 468127.00
. . .
. . .
In year ??, balance ????? reaches target 2000000.00"
And here is my sad code so far (sorry if the formatting looks weird):
/* CSCI 112; online class */
#include <stdio.h>
/* Week 6: Lab 2 - Program 2 (Retirement) */
void main(void) {
int year;
double balance, target, endbalance, deposit, rate, interest;
printf("Enter starting balance ($): ");
scanf("%lf", &balance);
printf("Enter amount deposited every year ($): ");
scanf("%lf", &deposit);
printf("Enter estimated annual interest rate (\%%): ");
scanf("%lf", &rate);
printf("Enter target balance ($): ");
scanf("%lf", &target);
year = 0;
interest = 0;
rate = rate / 100.0;
endbalance = balance + deposit + interest;
printf("\nYear Deposit Interest Balance");
printf("\n---- ------- -------- -------");
do {
endbalance = endbalance + deposit + interest;
printf("\n%d %.2lf %.2lf %.2lf", year, deposit, interest, endbalance);
year += 1;
interest = (endbalance + deposit) * rate;
} while (endbalance <= target);
printf("\nIn year %d, balance %.2lf reaches target %.2lf", year, balance, target);
}
Output:
Enter starting balance ($): 250000.0
Enter amount deposited every year ($): 20000.0
Enter estimated annual interest rate (%): 10.0
Enter target balance ($): 2000000.0
Year Deposit Interest Balance
---- ------- -------- -------
0 20000.00 0.00 290000.00
1 20000.00 31000.00 341000.00
2 20000.00 36100.00 397100.00
3 20000.00 41710.00 458810.00
4 20000.00 47881.00 526691.00
5 20000.00 54669.10 601360.10
6 20000.00 62136.01 683496.11
7 20000.00 70349.61 773845.72
8 20000.00 79384.57 873230.29
9 20000.00 89323.03 982553.32
10 20000.00 100255.33 1102808.65
11 20000.00 112280.87 1235089.52
12 20000.00 125508.95 1380598.47
13 20000.00 140059.85 1540658.32
14 20000.00 156065.83 1716724.15
15 20000.00 173672.42 1910396.57
16 20000.00 193039.66 2123436.22
In year 17, balance 250000.00 reaches target 2000000.00
I would really appreciate some feedback! :D
Thanks in advance!