Question

In: Computer Science

Enhance your program from Exercise 20 by continuing to ask the user for the interest rate...

Enhance your program from Exercise 20 by continuing to ask the user for the interest rate and loan amount. Then ask the user for the amount of principle they would like to payoff each month. Make sure that the user enters a positive number that is less than the loan amount. This means that the monthly payment will change from month to month. Use this information to produce a payment chart that will include as output the Payment (Month) Number, the Payment Amount (monthly amount + interest), and the interest for that month. Make sure to calculate the Payment Amount for the last month as the remaining Loan Amount + Interest. At the end of the payment chart, output the total interest paid.

Ask user for loan amount, interest rate and monthly principle payment.

Check to make sure the monthly principle payment is less than the loan amount.

Set month counter and total interest to zero

Repeat until loan amount is less than or equal principle payment

   Calculate interest as loan amount * interest rate

   Calculate payment as interest + principle payment

   Add interest to total interest

   Increment month counter

   print the month counter, payment amount and interest

   Subtract principle payment from loan amount

Calculate final interest = loan amount * interest rate

Calculate final payment = loan amount + final interest

increment the month counter

Add final interest to total interest

print the month counter, final payment and final interest

print the total interest

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
    double loanAmount;
    double interestRate;
    double interestRatePerMonth;
    double monthlyPayment;
    double paymentPrincipal;
    int months;

    cout << fixed << showpoint;
    cout << setprecision(2);

    cout << "Enter the loan amount: ";
    cin >> loanAmount;
    cout << endl;

    cout << "Enter the interest rate per year: ";
    cin >> interestRate;
    cout << endl;

    interestRatePerMonth = (interestRate / 100) / 12;

    cout << "Enter the monthly payment: ";
    cin >> monthlyPayment;

    if (monthlyPayment <= loanAmount * interestRatePerMonth)
    {
        cout << "Monthly payment is too low. The loan cannot be repaid."
            << endl;
        return 1;
    }

    months = 0;

    while (loanAmount > 0)
    {
        paymentPrincipal = monthlyPayment - (loanAmount * interestRatePerMonth);
        loanAmount = loanAmount - paymentPrincipal;
        months++;
    }

    cout << "It will take " << months << " months to repay the loan."
         << endl;

    return 0;
}

Solutions

Expert Solution

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
double loanAmount;
double interestRate;
double interestRatePerMonth;
double monthlyPayment;
double paymentPrinciple;
double totalInterest = 0;
double interest;
int months;

cout << fixed << showpoint;
cout << setprecision(2);

cout << "Enter the loan amount: ";
cin >> loanAmount;
cout << endl;

cout << "Enter the interest rate per year: ";
cin >> interestRate;
cout << endl;

interestRatePerMonth = (interestRate / 100) / 12;

cout << "Enter principle amount that will paid monthly: ";
cin >> paymentPrinciple;

if (paymentPrinciple >= loanAmount)
{
cout << "Principle payment should be less than loan amount"
<< endl;
return 1;
}

months = 0;

cout << left << setw(10) << "Month" << right << setw(15) << "Payment"
<< setw(15) << "Interest" << endl;

while (loanAmount > 0)
{
interest = loanAmount * interestRatePerMonth;
if(paymentPrinciple + interest > loanAmount)
monthlyPayment = loanAmount + interest;
else
monthlyPayment = paymentPrinciple + interest;

totalInterest += interest;
months++;
cout << left << setw(10) << months << right << setw(15) << monthlyPayment
<< setw(15) << interest << endl;
loanAmount = loanAmount - paymentPrinciple;
}

cout << "It will take " << months << " months to repay the loan."
<< endl;
cout << "Total interest amount paid = $" << totalInterest << endl;

return 0;
}


Related Solutions

C++ while loop Exercise Write a program that continues to ask the user to enter any...
C++ while loop Exercise Write a program that continues to ask the user to enter any set of numbers, until the user enters the number -1. Then display the total sum of numbers entered and their average. (note that you need to define a counter that counts how many numbers so the average = (sum/n) where n is your counter total. #include <iostream> using namespace std; int main() { int number, n=0, sum=0; cout << "Enter a number to start...
In your python program, ask the user to enter the annual income of an employee and...
In your python program, ask the user to enter the annual income of an employee and the years of experience. Pass these data to a function. The function decides if an employee does qualify for a loan or does not, then it prints a message based on the following rules: If the income is equal or greater than $40000, the function checks if the employee’s years of experience is greater than 4, if so the employee gets $6000 loan; otherwise,...
Create a program that will ask the user to choose their order from a simple menu....
Create a program that will ask the user to choose their order from a simple menu. Customers first choose from three types of dishes: Sandwiches/wraps, Rice Meals, or Noodles. They can type in 1, 2, or 3 to select the type then, they choose a specific dish as shown below. Confirm their order by displaying it on the screen after they make their selection. 1. Sandwiches/wraps Hamburger Chicken shawarma 2. Rice meals Arroz con pollo Chana masala 3. Noodles Chow...
in java code Modify your program as follows: Ask the user for the number of hours...
in java code Modify your program as follows: Ask the user for the number of hours worked in a week and the number of dependents as input, and then print out the same information as in the initial payroll assignment. Perform input validation to make sure the numbers entered by the user are reasonable (non-negative, not unusually large, etc). Let the calculations repeat for several employees until the user wishes to quit the program. Remember: Use variables or named constants...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will first enter a password, then enters the same password again; If the second input is the same as first one, the user successfully creates the password. Print “Well done.”; Otherwise, the user will be directed to repeat the whole process (go to step 1.)
Create a small program that contains the following. ask the user to input their name ask...
Create a small program that contains the following. ask the user to input their name ask the user to input three numbers check if their first number is between their second and third numbers
Write a program that will ask the user to enter the amount of a purchase. The...
Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 5 percent and the county sales tax is 2.5 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the...
Write a program that will ask for the user to input a filename of a text...
Write a program that will ask for the user to input a filename of a text file that contains an unknown number of integers. And also an output filename to display results. You will read all of the integers from the input file, and store them in an array. (You may need to read all the values in the file once just to get the total count) Using this array you will find the max number, min number, average value,...
// This program ask the user to enter a character. It then performs a // linear...
// This program ask the user to enter a character. It then performs a // linear search on a character array and display the number of times // that the character appears on the array. If the character is not in the // array, then it will display a message saying that is was not found. // Add the necessary code for the program to work. // NOTE: // You don't have to edit anything in the main(), just in...
Write a C++ program that allows a user choose item to purchase from a list; ask...
Write a C++ program that allows a user choose item to purchase from a list; ask for the amount to calculate the cost of the purchase; offer to add a tip; add a delivery fee based on the subtotal; then calculate the total amount that the user needs to pay. ---------------------------------- ----- GROCERY SHOPPING ITEMS ----- Milk     - $5.99 / gallon Egg      - $6.99 / dozen Cheese   – $10.98 / 8oz Pasta    – $2.75 / packet ---------------------------------- Other Values to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT