Write a program in C that does the following: Write a program that calculates ho
ID: 3563289 • Letter: W
Question
Write a program in C that does the following:
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 (in formatted columns and rows) 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. The output should look as follows:
Enter starting balance ($):
Enter amount deposited every year ($):
Enter estimated annual interest rate (%):
Enter target balance ($):
Year Deposit Interest Balance
---- ------- -------- -------
Please include a screenshot of a sample input/output. The program has to be in C. Thank you!
Explanation / Answer
Basically, the part of initial deposit has to be dealt with separately from the subsequent deposits, so i added the If...then to condition that, a few variables needed to be changed or added to accommodate the setup too.
#include<stdio.h>
void main (void)
{
int totalYears;
double balance=0; //changed
double deposit;
double startingDeposit; //added
double subsequentDeposit; //added
double interestRate;
double interest=0; //changed
double targetBalance;
printf("Enter starting balance ($): ");
scanf_s("%lf", &startingDeposit);
printf("Enter amount deposited every year ($): ");
scanf_s("%lf", &subsequentDeposit);
printf("Enter esitmated annual interest rate (%%): ");
scanf_s("%lf", &interestRate);
printf("Enter target balance ($): ");
scanf_s("%lf", &targetBalance);
totalYears = 0;
printf("%4s %10s %10s %10s ", "Year", "Deposit", "Interest", "Balance" );
printf("---- ------- -------- ------- ");
do {
if(totalYears>0){ //added and changed
deposit = subsequentDeposit;
interest = ((balance + deposit) * (interestRate/100));
balance = balance + interest;
}else{ //when totalYears=0
deposit = startingDeposit;
balance = balance + deposit;
}
printf("%4d %10.2lf %10.2lf %10.2lf ", totalYears, deposit, interest, balance);
++totalYears;
} while (balance <= targetBalance);
printf("In year %d, balance %.2lf reaches target 2000000.00 ", totalYears, balance);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.