-
-
Save quicksnap/6153146 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iomanip> | |
#include <string> | |
#include <cmath> | |
using namespace std; | |
// declaring the functions | |
double findInt( double intRate, int yearlyPayments ); | |
double loanAmountFunc( double i, double y, double periodicPayment ); | |
double monthlyPaymentFunc( double i, double y, double periodicPayment ); | |
int main() | |
{ | |
double salary; | |
double intRate; | |
int loanLength = -30; | |
int yearlyPayments = 12; | |
double periodicPayment = 0; | |
// input the salary and interest rate | |
cout << "Please enter your yearly gross salary: "; | |
cin >> salary; | |
cout << "Please enter an interest rate: "; | |
cin >> intRate; | |
// turn interest rate into a decimal | |
intRate = intRate * 0.01; | |
// find the PeriodicPayment | |
periodicPayment = ( ( salary / 12 ) * .3 ); | |
// Calculate "y" | |
double y = -( yearlyPayments * loanLength ); | |
// Call function to calculate "i" | |
double i = findInt( intRate, yearlyPayments ); | |
// Call function for monthly payment | |
double monthlypayment = monthlyPaymentFunc( i, y, periodicPayment ); | |
// Call function for loan amount | |
double loanAmount = loanAmountFunc( i, y, periodicPayment ); | |
// two decimal precision | |
cout.precision(2); | |
cout << fixed << showpoint; | |
// Output to Terminal | |
cout << "The target (30% of monthly salary) monthly payment range is: $" << ( periodicPayment - 10 ) | |
<< " - \n$" << periodicPayment << endl; | |
cout << "Your max target house price is: $" << loanAmount << endl; | |
cout << "with a monthly payment of: $" << monthlypayment << endl; | |
system( "pause" ); | |
} | |
// i Function | |
double findInt( double intRate, int yearlyPayments ) | |
{ | |
double i = 0; | |
i = intRate / yearlyPayments; | |
return i; | |
} | |
// Loan Amount Function | |
double loanAmountFunc( double i, double y, double periodicPayment ) | |
{ | |
double loanAmount = 0; | |
double monthlyPayment = 0; | |
while ( periodicPayment > monthlyPayment && monthlyPayment < ( periodicPayment - 10 ) ) | |
{ | |
loanAmount += 2000; | |
monthlyPayment = ( loanAmount * i / ( 1 - pow ( ( 1 + i ),-( y ) ) ) ); | |
} | |
return loanAmount; | |
} | |
// Monthly Payment Function | |
double monthlyPaymentFunc( double i, double y, double periodicPayment ) | |
{ | |
double loanAmount = 0; | |
double monthlyPayment = 0; | |
while ( periodicPayment > monthlyPayment && monthlyPayment < ( periodicPayment - 10 ) ) | |
{ | |
loanAmount += 2000; | |
monthlyPayment = ( loanAmount * i / ( 1 - pow ( ( 1 + i ),-( y ) ) ) ); | |
} | |
return monthlyPayment; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment