#include <iostream>
#include <iomanip>
using namespace std;
int main() {
// Ask for input
double annual_interest_rate, starting_balance;
int months_passed;
cout << "Enter the annual interest rate: ";
cin >> annual_interest_rate;
cout << "Enter the starting balance: ";
cin >> starting_balance;
cout << "Enter the number of months that have passed: ";
cin >> months_passed;
// Initialize variables
double balance = starting_balance;
double total_deposits = 0, total_withdrawals = 0, total_interest = 0;
// Loop for each month
for (int i = 1; i <= months_passed; i++) {
// Ask for input
double deposit, withdrawal;
cout << "\nMonth " << i << "\n";
cout << "Enter the amount deposited: ";
cin >> deposit;
while (deposit < 0) {
cout << "Amount must be non-negative. Enter again: ";
cin >> deposit;
}
cout << "Enter the amount withdrawn: ";
cin >> withdrawal;
while (withdrawal < 0) {
cout << "Amount must be non-negative. Enter again: ";
cin >> withdrawal;
}
// Update balance and totals
balance += deposit - withdrawal;
total_deposits += deposit;
total_withdrawals += withdrawal;
// Calculate interest
double monthly_interest_rate = annual_interest_rate / 12;
double monthly_interest = balance * monthly_interest_rate;
total_interest += monthly_interest;
balance += monthly_interest;
// Check if account has been closed
if (balance < 0) {
cout << "\nYour account has been closed due to a negative balance.\n";
break;
}
}
// Display results
cout << fixed << setprecision(2);
cout << "\nEnding balance: $" << balance << endl;
cout << "Total deposits: $" << total_deposits << endl;
cout << "Total withdrawals: $" << total_withdrawals << endl;
cout << "Total interest earned: $" << total_interest << endl;
return 0;
}