This program is not a C++ program but a program that should be writted in codebl
ID: 3531279 • Letter: T
Question
This program is not a C++ program but a program that should be writted in codeblocks. This means that the #include <stdio.h> headline would be used and so forth. A new college graduate would like to weigh the merits of saving regularly. Write a program that calculates the balance of a savings account receiving regular contributions and NO withdrawals at the end of a period of time. The program should ask the user for annutal interest rate and the amount to be contributed monthluy. (Do not accept negative numbers).
Use nested loops to calculate the balance in the account after 30 years. The outer loop should iterate through the years, printing the year number and account balance to the monitor after each year. Your output should be in columns with headings. The innter loop should iterate through the months in a year. For each month you should calculate the interest (divide the annual rate by 12, multiply by the current balance), add the interest to the monthly savings account balanc and then add the monthly contribution too. Keep seperate running totals of th total money deposited by the graduate and the total interest earned. After the last iteration, the program should display the ending balance, the toal amount of money deposited by the graduate and the total interest earned. Use seperate functions to: Read the annual interest rate, Read the monthly contribution, Calculate the interest, Display the ending balance, total deposits and total interest earned. You may put the nested loops, the logic to keep a running total of the deposits and interest earned and the statements to display the year and current balance in main.
Explanation / Answer
//something like this:
#include<stdio.h>
double getAnnualRate(){
double AnnualRate;
do{
printf("Enter the annual interest rate( decimal form): ");
scanf("%lf",&AnnualRate);
if(AnnualRate<0)
printf("The interest rate must be positive. ");
}while(AnnualRate<0);
return AnnualRate;
}
double getMContribution(){
double monthlyContribution;
do{
printf("Enter a positive monthly contribution amount: ");
scanf("%lf",&monthlyContribution);
}while(monthlyContribution<0);
return monthlyContribution;
}
double calcInterest(double balance, double AnnualRate){
return balance*(AnnualRate/12.0);
}
void display(double endingBalance,double depositTotal,double interestTotal){
printf("The end balance is $%.2lf, of which $%.2lf was deposited and $%.2lf was from interest.",endingBalance,depositTotal,interestTotal);
return;
}
int main(){
double balance=0, monthlyContribution,AnnualRate,interest;
double depositTotal=0, interestTotal=0;
int i,j;
AnnualRate=getAnnualRate();
monthlyContribution=getMContribution();
printf("Year Balance ");
for(i=0;i<=30;i++){
for(j=0;j<12;j++){
interest=calcInterest(balance,AnnualRate);
depositTotal+=monthlyContribution;
interestTotal+=interest;
balance+=interest+monthlyContribution;
}//end for j
printf("%d %.2lf ",i,balance);
}
display(balance,depositTotal,interestTotal);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.