SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Flow Of Control
Exercise:
Programming Projects
Question:2 | ISBN:9780132846813 | Edition: 5

Question

You have just purchased a stereo system that cost $1,000 on the following credit plan: no down payment, an interest rate of 18% per year (1.5% per month), and monthly payments of $50. The monthly payment of $50 is used to pay the interest, and whatever is left is used to pay part of the remaining debt. Hence, the first month you pay 1.5% of $1,000 in interest. That is $15 in interest. The remaining $35 is deducted from your debt, which leaves you with a debt of $965.00. The next month you pay interest of 1.5% of $965.00, which is $14.48. Hence, you can deduct $35.52 (which is $50 - $14.48) from the amount you owe.

Write a program that will tell you how many months it will take you to pay off the loan, as well as the total amount of interest paid over the life of the loan. Use a loop to calculate the amount of interest and the size of the debt after each month. (Your final program need not output the monthly amount of interest paid and remaining debt, but you may want to write a preliminary version of the program that does output these values.) Use a variable to count the number of loop iterations and hence the number of months until the debt is zero. You may want to use other variables as well. The last payment may be less than $50 if the debt is small, but do not forget the interest. If you owe $50, then your monthly payment of $50 will not pay off your debt, although it will come close. One month's interest on $50 is only 75 cents.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

#include <iostream>  
using namespace std;  
 
int main() {  
 
int months = 0;  
double totalCost = 0;  
double yearlyInterest = 0;  
double monthlyPayment = 0;  
double interestPaymentMonth = 0;  
double interestPercentPerMonth = 0;  
double principlePerMonth = 0;  
double totalInterest = 0.0;  
double principle = 0.0;  
cout.precision(5);  
 
cout << "How much money was used?: $";  
cin >> totalCost;  
cout << "What was the yearly interest rate as a percentage: ";  
cin >> yearlyInterest;  
 
interestPercentPerMonth = (double)yearlyInterest / 12;  
interestPaymentMonth = totalCost * (double)interestPercentPerMonth / 100;  
 
cout << "Enter a monthly payment amount greater than $" << interestPaymentMonth << " : $";  
cin >> monthlyPayment;  
 
while (monthlyPayment < interestPaymentMonth)  
{  
cout << "Enter a monthly payment amount greater than $" << interestPaymentMonth << " : $";  
}  
cout << endl;  
 
while (totalCost > 0)  
{  
interestPercentPerMonth = (double)yearlyInterest / 12;  
interestPaymentMonth = totalCost * (double)interestPercentPerMonth / 100;  
totalInterest = totalInterest + interestPaymentMonth;  
principlePerMonth = monthlyPayment - interestPaymentMonth;  
totalCost = totalCost - principlePerMonth;  
principle = principle - principlePerMonth;  
if (totalCost < monthlyPayment && totalCost > 0)  
{  
cout << "The remaining loan amount is to be paid on the last month which will be: " << totalCost << endl;  
}  
months++;  
}  
cout << "It will take you " << months - 1 << " months to repay the loan." << endl;  
cout << "The total Interest paid for the loan was: $" << totalInterest << endl;  
return 0;  
}  

 

0 0

Discussions

Post the discussion to improve the above solution.