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

Was this article helpful?
👍 YesNo

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

  1. Avatar Lee says:

    What is the use of those highlighted with yellow? Sorry, Im a beginner that’s why I dont know much about c++

  2. Avatar edgar chicas says:

    Can you please explain this: totBalance+=(totBalance*monthlyInterestRate);

    • admin admin says:

      That expression (located on line #50 in the code) literally means:

      totBalance = totBalance + (totBalance*monthlyInterestRate);

      This operation is taking your current total balance, and adding to it the calculated interest received for that given month.

      This specification can be found in the following program requirements listed above the source code.

      After each loop iteration, the monthly interest rate is multiplied by the current balance, which is then added to the total balance.

      Hope this helps.

      • Avatar jesuswithme says:

        why you don’t put the first formula of interest after the second formula of balance ?

        totBalance += (totBalance*monthlyInterestRate);

        totInterest += (totBalance*monthlyInterestRate);

  3. Avatar hopeless says:

    I’m working on the exact same program yet my results are wildly incorrect, can I show my code and get some help fixing it?

  4. Avatar Jimmy cartwell says:

    This is hard my interest one asks me to average the beginning and end of the month totals. I’m very confused

Leave a Reply