Skip to content

Instantly share code, notes, and snippets.

@Elsayegh
Last active August 2, 2018 21:46
Show Gist options
  • Select an option

  • Save Elsayegh/0546419b990e8b6bbf74266b205d37c8 to your computer and use it in GitHub Desktop.

Select an option

Save Elsayegh/0546419b990e8b6bbf74266b205d37c8 to your computer and use it in GitHub Desktop.
C++ Present Value (C++ functions)
/*Suppose you want to deposit a certain amount of money into a savings account and then leave it alone to draw interest
for the next 10 years. At the end of 10 years you would like to have $10000 in the account. How much would you need to deposit today
toi make that happen? You can use the following formula which is known as the percent value formula, to find out:
F
P = -----
(1 + r) (to the power of n)
P - Present value or amount that you need to deposit today
F - Future value that you want in the account (in this case, $10,000)
r - annual interest rate
n - number of years you plan to let the money sit in the account
Write a program that has a function named presentValue that performs this calculation. The function should accept the future value,
annual interest rate, and number of years in the arguments. It should return the present value, which is the amount that you need to
deposit today.
Demonstrate the function in a program that lets the user experiment with different values for the formula's terms.
*/
#include <iostream>
#include <conio.h>
#include <ctime>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <string>
#include <math.h>
using namespace std;
double presentValue(double, double, int);
int main() {
double aimedValue;
double annualRate;
int totalYears;
double deposite;
cout << "How much you are aiming to have in your saving? " << endl;
cin >> aimedValue;
cout << "how much is the annual rate? " << endl;
cin >> annualRate;
cout << "and for how many years? " << endl;
cin >> totalYears;
deposite = presentValue(aimedValue, annualRate, totalYears);
cout << "You need " << deposite << " at the moment to make " << aimedValue << " in " << totalYears << " years" << endl;
_getch();
}
double presentValue(double futureValue, double annInterest, double years) {
double recentValue;
recentValue = futureValue / (pow(1 + annInterest), years);
return recentValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment