+1 (315) 557-6473 

Intrest Homework Solution using C++


Problem

Create a program that calculates twelve months’ interest and principal loan balance. Your program will ask the user for the starting principal balance, annual interest rate, and monthly payment amount.

Your program will verify the input is reasonable, as described in the Error Checking section below. After obtaining reasonable input, the program will process 12 months of payments. The output will show the accrued interest and the updated loan balance after each month’s payment. The total interest paid and total payment amounts for the 12 month period will also be calculated and displayed.

Your program output should resemble the output below.

Intrest

Solution:

/* Class: ELEC 1520 * Assignment: * Program author: * Date: * Tested on * C++ Environment: * Operating system: * Description: A program that calculates twelve month¿s interest and principle loan balance. */ #include #include #include using namespace std; int main() { // Declare the maximum allowed loan amount constant const int max_loan = 100000; // Declare the minimum allowed loan amount constant const int min_loan = 500; // Declare the user input variables, loan, annual interest rate and monthly payment double loan, annual_rate, payment; // Declare calculated variable for the monthly interest rate double monthly_rate; // Ask the user for the starting principle balance. cout << "Enter loan amount [500.00 100000.00]: "; cin >> loan; // Check the state of the cin input stream object. // The fail bit is set when cin fails to read input.Terminate the program when scanf fails. if (cin.fail() == true) { cout << "Programming terminating due to input error" << endl; exit(EXIT_FAILURE); } // If the loan is out of the defined range, terminate the program with the error message else if (loan < min_loan || loan > max_loan) { cout << "The loan should be in the range of " << min_loan << " to " << max_loan << "." << endl; exit(EXIT_FAILURE); } cout << endl; // Ask the user for annual interest rate cout << "Enter annual interest rate: "; cin >> annual_rate; // Check the state of the cin input stream object. // The fail bit is set when cin fails to read input.Terminate the program when scanf fails. if (cin.fail() == true) { cout << "Programming terminating due to input error" << endl; exit(EXIT_FAILURE); } // If the annual interest rate is out of the defined range, terminate the program with the error message else if (annual_rate < 0.00 || annual_rate > 8.99) { cout << "The annual interest rates should be in the range of 0.00% to 8.99%." << endl; exit(EXIT_FAILURE); } cout << endl; monthly_rate = annual_rate / 100.00 / 12.0; // Ask the user for the monthly payment amount cout << "Enter monthly payment: "; cin >> payment; // Check the state of the cin input stream object. // The fail bit is set when cin fails to read input.Terminate the program when scanf fails. if (cin.fail() == true) { cout << "Programming terminating due to input error." << endl; exit(EXIT_FAILURE); } // The monthly payment amounts should be positive. else if (payment < 0) { cout << "Monthly payment amounts should be positive." << endl; exit(EXIT_FAILURE); } // The monthly payment amount entered must be greater than the first month's interest. else if (payment < (loan * monthly_rate)) { // If the monthly payment input value is not greater than the first month's interest, // display an appropriate error messageand terminate the program. cout << "The monthly payment amount entered must be greater than the first month's interest." << endl; exit(EXIT_FAILURE); } cout << endl; // Print the header for the monthly payment schedule cout << endl; cout << " Begin Monthly End" << endl; cout << " Loan Accrued Payment Loan" << endl; cout << " Month Balance Interest Amount Balance" << endl; cout << endl; // Declare the variable for the monthly interest double interest; // Declare the variable for the start of period loan balance double balance = loan; // Declare the variable for the end of period loan balanace double new_balance; // Declare the variable for the accured interest double accured_interest = 0; // Declare the variable for the accured payment double accured_payment = 0; // Using the for-loop to iterate to print the 12-month payment schedule // If the end loan balance is equal or less than 0, exit the loop for (int i = 0; balance > 0 && i < 12; i++) { // Compute the monthly interest // It makes use of round function to correct the amount into 2 decimal place interest = round( balance * monthly_rate * 100 ) / 100.0; // Accumulate the interest for the accured interest accured_interest += interest; // If the payment is larger than the sum of the balance and interest, correct the payment // to be the sum of the balance and interest if ( payment > ( balance + interest ) ) { payment = balance + interest; } // Accumulate the payment for the accured payment accured_payment += payment; // Calculate the end of period loan balance new_balance = balance - payment + interest; // Display the current period payment schedule cout << setw(5) << (i + 1) << setw(13) << fixed << setprecision(2) << balance << setw(14) << interest << setw(14) << payment << setw(14) << new_balance << endl; // Update the balance with the calculated end of period loan balance balance = new_balance; } cout << endl; // Print the total amount for the interest and payment paid cout << "Amount Total" << setw(20) << accured_interest << setw(14) << accured_payment << endl; cout << endl; }