Daily Archives: April 4, 2012

C++ || Savings Account Balance – Calculate The Balance Of A Savings Account At The End Of A Period

Here is another actual homework assignment which was presented in an intro to programming class. The following program was a question taken from the book “Starting Out with C++: Early Objects (7th Edition),” chapter 5, problem #16.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

For Loops - How To Use Them
Assignment Operators - What Are They?
Setprecision - What Is It?
Interest Rate

This program first prompts the user to enter the annual interest rate, starting balance, and the number of months which has passed since the account was established. Upon obtaining the information, a loop is then used to iterate through each month, performing the following:

• Ask the user for the amount deposited into the account during the month (positive values only). This amount is added to the balance.

• Ask the user for the amount withdrawn from the account during the month (positive values only). This is subtracted from the balance.

• Calculate the monthly interest. The monthly interest rate is the annual interest rate divided by 12. After each loop iteration, the monthly interest rate is multiplied by the current balance, which is then added to the total balance.

After the last iteration, the program displays the ending balance, the total amount of deposits, the total amount of withdrawals, and the total interest earned.

If a negative balance is calculated at any point, an erroneous message is displayed to the screen indicating that the account has been closed, and then the program terminates.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output

Enter the annual interest rate on the account (e.g .04): .07
Enter the starting balance: $5000
How many months have passed since the account was established? 3

Month #1
Total deposits for this month: $500
Total withdrawal for this month: $120

Month #2
Total deposits for this month: $750
Total withdrawal for this month: $200

Month #3
Total deposits for this month: $500
Total withdrawal for this month: $100

Ending balance:................. $ 6433.47
Amount of deposits:............. $ 1750.00
Amount of withdrawals:.......... $ 420.00
Amount of interest earned:...... $ 103.47