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

Print Friendly, PDF & Email

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

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

  1. Lee says:

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

    • admin admin says:

      Hello, thanks for visiting!

      The highlighted lines merely represent the code that is essential to completing the task of this program

  2. 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.

      • jesuswithme says:

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

        totBalance += (totBalance*monthlyInterestRate);

        totInterest += (totBalance*monthlyInterestRate);

Leave a Reply