Skip to content

Instantly share code, notes, and snippets.

@NanxingLAI
Last active January 20, 2019 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NanxingLAI/972171bdb3629783e42ea001e26cbcb4 to your computer and use it in GitHub Desktop.
Save NanxingLAI/972171bdb3629783e42ea001e26cbcb4 to your computer and use it in GitHub Desktop.
task_3_exam
// main.cpp
// Final_Nanxing_Task3
//
// Created by Nanxing LAI on 2019/1/20.
// Copyright © 2019 Mac. All rights reserved.
//
#include<iostream>
#include<cmath>
#include<string>
#include<iomanip>
using namespace std;
// the test of double value number, which inluding r, pmt and pv
double inputDouble(string prompt){
cout << prompt;
double value;
cin >> value;
while(value < 0){
cout << "invalid, " << prompt;
cin >> value;
}
return value;
}
int main()
{
double r,pv,pmt,a,b,c,d;
int n;
// providing values
r = inputDouble("please input periodic interest rate: ");
pmt = inputDouble("please input the instalment: ");
pv=inputDouble("plese input the present value: ");
n=1;
a=pmt/pv;
b=r*(1+1/(pow(1+r,n)-1));
c=b-a;
d=abs(c);
while(d>0.001)
{
n=n+1;
b=r*(1+1/(pow(1+r,n)-1));
c=b-a;
d=abs(c);
}
cout<<"the number of period is "<<n<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment