1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include "stdafx.h"
#include <stdio.h>
#include <math.h>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
void calculationAmort(float P, float interestYear, int ny);
void printSch(int i, float P, float monthPaid, float interestPaid, float principalPaid, float newBalance);
double RoundedPct(double interestMonth)
{
double Eighths[] = {0.0, .125, .25, .375, .5, .625, .75, .875, 1.0};
int wholePct;
double fracPct;
int i;
wholePct = (int)(100.0 * interestMonth);
fracPct = 100.0 * interestMonth - wholePct;
for (i = 0; i < 8; i++) {
if (Eighths[i] <= fracPct && fracPct <= Eighths[i + 1])
{
if (fracPct - Eighths[i] <= Eighths[i + 1] - fracPct)
fracPct = Eighths[i];
else fracPct = Eighths[i + 1];
break;
}
}
interestMonth = (wholePct + fracPct)/100.0;
return interestMonth;
}
void calculationAmort(float P, float interestYear, int ny)
{
float newBalance;
float interestMonth = (interestYear/1200);
float interestPaid, principalPaid, monthPaid;
int i;
int nm=ny*12;
for(i=1;i<nm;i++)
{
interestPaid = P*interestMonth;//interest paid
principalPaid = monthPaid-interestPaid; //princial paid
newBalance = P-principalPaid; //new balance
printSch(i,P,monthPaid,interestPaid,principalPaid,newBalance); //print amortization table
P = newBalance; //update old balance
}
}
void printSch(int i, float P, float monthPaid, float interestPaid, float principalPaid, float newBalance)
{
cout << std::setprecision(5) << fixed;
cout<<i<<"\t"<<P<<"\t\t"<<monthPaid<<"\t\t"<<interestPaid<<"\t\t"<<principalPaid<<"\t\t"<<newBalance<<"\n";
}
double getPaymentAmount(int months, double principal, double interest)
{
double Payment;
return Payment = (pow (1 + interest, months))/(pow (1 + interest, months-1)) * principal * interest;
}
double getPrincipalAmount(int months, double payment, double interest)
{
double Principal;
return Principal = (pow (1 + interest, months-1))/(pow (interest * (1 + interest, months))) * payment;
}
int getNumberOfMonths(double payment, double principal, double interest)
{
int Months;
return Months = (log(payment)-log(payment-(principal*interest)))/log(1+interest);
}
|