Author Topic: Let's discuss this code sample  (Read 2968 times)

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
Let's discuss this code sample
« on: November 16, 2005, 05:45:20 PM »
Code: [Select]

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
   
    // define a constant conversion factor
const int MONTHS_IN_YEAR = 12;

//declare variables to hold the data
float loanAmount, annualInterestRate;
    int loanPeriod;
double monthlyPayment, totalPayment;
double monthlyInterestRate;
int numberOfPayments;

    cout << " -- LOAN CALCULATOR -- " << endl;
cout << "This program computes the monthly and total " << endl;
cout << "payments for a give loan amount, annual " << endl;
cout << "interest rate, and loan period. " << endl << endl;
   
    cout << "Loan Amount  :   ";
    cin >> loanAmount;
cout << "Annual interest Rate e.g. 9.5  :  ";
cin >> annualInterestRate;
    cout << "Loan Period # of years :   ";
cin >> loanPeriod;

monthlyInterestRate  = annualInterestRate / 100 / MONTHS_IN_YEAR;
numberOfPayments = loanPeriod * MONTHS_IN_YEAR;

monthlyPayment = (loanAmount * monthlyInterestRate) /
( 1 - pow(1 / (1 + monthlyInterestRate), numberOfPayments) );
totalPayment = monthlyPayment * numberOfPayments;

cout << "For " << endl
<< "Loan Amount: " << loanAmount << endl
<< "Annual Interest Rate: " << annualInterestRate << "%" << endl
<< "Loan Period (years): " << loanPeriod << endl
         << endl
<< "Monthly Payment   : " << monthlyPayment << endl
<< "  Total Payment   : " << totalPayment << endl;
   
    system("PAUSE");
    return 0;
}
Analyze. Design. Develop. Debug. Deploy. Maintain.