/* * A new version of our interest rate program that compounds interest * monthly, rather than yearly. */ #include #include int main() { double yearEndBalance(double monthly_bal, double interest_rate); int period; /* length of period */ int year; /* year of period */ double balance; /* balance at end of year */ double intrate; /* interest rate */ printf("Enter interest rate, principal, and period: "); scanf("%lf %lf %i", &intrate, &balance, &period); printf("Interest Rate: %7.2f%%\n", intrate * 100); printf("Starting Balance: $ %7.2f\n", balance); printf("Period: %7i years\n\n", period); printf("Year Balance\n"); for (year = 1; year <= period; year = year + 1) { balance = yearEndBalance(balance, intrate); printf("%4i $ %7.2f\n", year, balance); } return EXIT_SUCCESS; /* assume program succeeded */ } /* Compute a year's ending balance, compounding interest monthly */ double yearEndBalance(double monthly_bal, double interest_rate) { double monthly_intrate; /* % interest per month */ int month; /* current month */ monthly_intrate = interest_rate / 12; for (month = 1; month <= 12; month = month + 1) monthly_bal = monthly_bal * monthly_intrate + monthly_bal; return monthly_bal; }