Wednesday, January 4, 2017

Programming Challenge 6.9 - Present Value

/* Present Value - 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
   $10,000 in the account. How much do you need to deposit today to
   make that happen? The following formula can be used to find out.
 
   * P = F / (1 + r)^n
 
   The terms in the formula are as follows:
 
   * P is the present value, or the amount you need to deposit today.
   * F is the future value that you want in the account. (In this case,
     F is $10,000.
   * r is the annual interest rate.
   * n is the number of years you plan to let the money sit in the
          account.
           
   This program uses a function named presentValue that performs
   this calculation. The program is demonstrated by letting the
   user experiment with different values for the formula's terms. */

#include "Utility.h"

/* Prototype: Present value */
double presentValue(double, double, int);

int main()
{
   /* Constants: Begin, Try again, Quit */
   const int BEGIN = 1,
             AGAIN = 2,
             QUIT = 3;

   /* Variable: Selection */
   int selection = 0;

   /* Variables: Future Value, Annual Interest Rate, PresentValue */
   double futureValue = 0.0,
          annualIntRate = 0.0,
          presentVal = 0.0;

   /* Variable: Number of years */
   int numYears = 0;

   /* Display: Introduction */
   cout << "\t\tAshikaga Bank - Personal Financal Planner\n\n"
        << "Valued customer,\n\n"
        << "Suppose you wish to know how much money you need to deposit\n"
        << "today, to receive the sum you wish some time in the future.\n\n"
        << "For instance:\n"
        << "If you wish to have $10,000 in your savings account after 10\n"
        << "years, with an interest rate of 5.25% annualy, you would have\n"
        << "to invest a certain amount of money today.\n\n"
        << "This program calculates the so called present value, or amount\n"
        << "of money you need to put into your savings account today, to\n"
        << "receive the sum of money you wish for in the future. It also\n"
        << "allows you to experiment with the following values to get\n"
        << "different results:\n\n"
        << "* Future Value: The value you wish to receive\n"
        << "* Annual Interest Rate: For instance 5.25%\n"
        << "* The number of years you plan to let the money\n"
        << "  alone on your account to earn interest.\n\n";

   do
   {
      /* Display: Menu
         Get: User selection */
      cout << "1. Begin\n"
           << "2. Try again\n"
           << "3. Quit.\n";
      cin >> selection;

      /* Validate: Menu selection */
      while (selection < 1 || selection > 3)
      {
         cout << "Please select a valid menu item: ";
         cin >> selection;
      }

      /* As long as the user does not wish to quit experimenting
      with different values, he or she is asked to enter the future
      value, the annual interest rate, and number of years. */
      if (selection != QUIT)
      {
         cout << "\nStep 1: Enter the future value you want in your\n"
            << "savings account: $ ";
         cin >> futureValue;

         /* Validate: Input */
         while (futureValue <= 0)
         {
            cout << "\nValued customer, you seem to have entered a\n"
                 << "negative number for future value. Please repeat\n"
                 << "your input (positive values only): $ ";
            cin >> futureValue;
         }

         cout << "\nStep 2: Now enter the annual interest rate that\n"
            << "applies to your savings plan (ex: 5.7): % ";
         cin >> annualIntRate;

         /* Validate: Input */
         while (annualIntRate <= 0 || annualIntRate > 100)
         {
            cout << "\nValued customer, the annual interest rate can not\n"
                 << "be lower than 1, and not greater than 100%.\n"
                 << "Please repeat your input: % ";
            cin >> annualIntRate;
         }

         cout << "\nStep 3: The last thing you need to enter is the\n"
            << "number of years you plan to let the money sit in\n"
            << "your savings account: ";
         cin >> numYears;

         /* Validate: Input */
         while (numYears <= 0)
         {
            cout << "\nValued customer, the input for years must be\n"
                 << "both positive, and at least equal to 1 or above.\n"
                 << "Please repeat your input: ";
            cin >> numYears;
         }

         annualIntRate /= 100;

         /* Call: presentValue */
         presentVal = presentValue(futureValue, annualIntRate, numYears);

         /* Set up: Numeric output formatting */
         cout << fixed << showpoint << setprecision(2);

         /* Display: Present value */
         cout << "Present Value Calculator - Result\n\n"
              << "\nValued customer,\n\n"
              << "According to the information you provided, you would\n"
              << "need to put $" << presentVal << " into your "
              << "account today to earn\n"
              << "$" << futureValue
              << " after " << numYears << " years "
              << " at an annual interest rate\n"
              << "of " << (annualIntRate * 100) << "%\n\n";
      }

      /* Display: When the user decides to quit, a message is displayed */
      if (selection == QUIT)
      {
         cout << "\nValued Customer,\n\n"
              << "Thank you for your trust in our services.\n"
              << "We wish you a successful future, and hope\n"
              << "that you will return soon.\n"
              << "Your Ashikaga Bank Team\n\n";
      }
   } while (selection != QUIT);

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: presentValue

   This function accepts the future value (F), the annual
   interest rate (r), and number of years (n) as arguments.
   It returns the present value, which is the amount needed
   to deposit today.
   ********************************************************** */

double presentValue(double futureValue, double annualIntRate,
                    int numYears)
{
   /* Variable: Present Value */
   double presentVal = 0.0;

   /* Calculation: The present value */
   presentVal = futureValue / pow(1.0 + annualIntRate, numYears);

   /* Return: The present value */
   return presentVal;
}

No comments:

Post a Comment