Your task is to implement this:
• A member variable that will hold the annual interest rate of the loan. Its default value will be 2.5.
• A member variable that will hold the number of years for the loan. Its default value will be 1.
• A member variable that will hold the loan amount. Its default variable will be 1000.
• A default constructor.
• Another constructor that has interest rate, years, and loan amount as its parameters.
• A member function that returns the annual interest rate of this loan.
• A member function that returns the number of the years of this loan.
• A member function that returns the amount of this loan.
• A member function that returns the monthly payment of this loan.
• A member function that returns the total payment of this loan.
Enter yearly interest rate, for example 8.25: 10
Enter number of years as an integer, for example 5: 5
Enter loan amount, for example 120000.95: 300000
The monthly payment is 6374.11
The total payment is 382446.80
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;
class Loan {
double interest;
int years;
double amount;
public:
Loan();
Loan(double interest, int years, double amount);
double getInterest();
int getYears();
double getAmount();
double getMonthlyPayment();
double getTotalPayment();
};
Loan::Loan()
{
interest = 2.5;
years = 1;
amount = 1000;
}
Loan::Loan(double interest, int years, double amount)
{
this->interest = interest;
this->years = years;
this->amount = amount;
}
double Loan::getInterest()
{
return interest;
}
int Loan::getYears()
{
return years;
}
double Loan::getAmount() {
return amount;
}
double Loan::getMonthlyPayment() {
int n = years*12;
double r = interest * 0.01 / 12;
double payment = amount;
payment /= pow(1+r, n) - 1;
payment *= r * pow(1+r, n);
return payment;
}
double Loan::getTotalPayment() {
double total = getMonthlyPayment();
total *= years*12;
return total;
}
int main() {
double interest, amount;
int years;
cout << "Enter yearly interest rate, for example 8.25: ";
cin >> interest;
cout << "Enter number of years as an integer, for example 5: ";
cin >> years;
cout << "Enter loan amount, for example 120000.95: ";
cin >> amount;
Loan loan(interest, years, amount);
double monthly = loan.getMonthlyPayment();
double total = loan.getTotalPayment();
cout << fixed << setprecision(2);
cout << "The monthly payment is " << monthly << endl;
cout << "The total payment is " << total << endl;
return 0;
}
Comments
Leave a comment