Created
January 20, 2019 09:17
-
-
Save NanxingLAI/20f1f2375e6c1a7b18b3a20398429414 to your computer and use it in GitHub Desktop.
task_2_exam
This file contains 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
// main.cpp | |
// Final_Nanxing_Task2 | |
// | |
// 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 check of interger number, only for the number of periods | |
int inputInteger(string prompt){ | |
cout << prompt; | |
int n; | |
cin >> n; | |
while(n < 1){ | |
cout << "invalid, " << prompt; | |
cin >> n; | |
} | |
return n; | |
} | |
// 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; | |
int n; | |
// providing values | |
n = inputInteger("please input number of periods: "); | |
r = inputDouble("please input periodic interest rate: "); | |
pmt = inputDouble("please input the instalment: "); | |
a=pow(1+r,n); | |
b=r*(1+1/(a-1)); | |
pv=pmt/b; | |
cout<<"the present value is "<<pv<<endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment