Compound Interest

Compound Interest

Introduction

This small application shows how to calculate the compound interest of a saving.

Prerequisites:

When you deposit money in a savings account, your money earns interest that is calculated every month or quarter, etc. Because this is not money you need right away, the amount accrued can be reinvested, thus boosting your interest so the next calculation would be based on the original amount plus the new added value. This is the basis of compound interest.

The compound interest can be calculated monthly or quarterly, etc based on the original amount you deposited, the interest rate, and the period you and the institution agreed upon.

Creating the Application

This application uses a dialog box equipped with the necessary controls used to perform the type or related calculated. The formula we will use to perform the calculations is as follows:

Compound Interest Formula P = Principal
r = Annual (Interest) Rate
m = Number of Compounding Periods per Year
n = Total Number of Compounding Periods
A = Amount Earned After n periods

 

 

 

Practical Learning: Starting the Exercise

  1. Start Microsoft Visual C++
  2. Create a new project named CompoundInterest1
  3. Create the project as a Dialog Based and set the Dialog Title as Compound Interest
  4. While still using the wizard:
    Change the application's Class Name to CExerciseApp
    Change the dialog's Class Name to CExerciseDlg
    Change the dialog's header file to ExerciseDlg.h
    Change the dialog's source file to ExerciseDlg.cpp
  5. Delete the TODO line and the OK button. Change the Caption of the Cancel button to Close
  6. Design the dialog box as follows:
     
    Application Design
    Control ID Caption Additional Properties
    Group Box Group Box   Values  
    Static Text Static Text   Principal: ...............  
    Edit Box IDC_PRINCIPAL   Align Text: Right
    Static Text   Interest: ...............  
    Edit Box IDC_ANNUAL_RATE   Align Text: Right
    Static Text   Number of Periods  
    Edit Box IDC_NUMBER_OF_PERIODS   Align Text: Center
    Group Box   Compound Frequency  
    Radio Button IDC_COMPOUND Monthly Group: True
    Left Text: True
    Radio Button   Quarterly Left Text: True
    Radio Button   Semiannually Left Text: True
    Radio Button   Annually Left Text: True
    Group Box      
    Static Text   Interest Earned  
    Edit Box IDC_INTEREST_EARNED   Align Text: Right
    Static Text   Amount Earned  
    Edit Box IDC_AMOUNT_EARNED   Align Text: Right
    Button IDC_CALCULATE_BTN C&alculate Default Button: True
  7. Using either the ClassWizard or the Add Member Variable Wizard, add the following member variables to the controls:
     
    Identifier Type Value Variable
    IDC_AMOUNT_EARNED CString m_AmountEarned
    IDC_ANNUAL_RATE CString m_AnnualRate
    IDC_COMPOUND int m_Compound
    IDC_INTEREST_EARNED CString m_InterestEarned
    IDC_NUMBER_OF_PERIODS CString m_InterestEarned
    IDC_PRINCIPAL CString m_Principal
  8. Initialize controls in the constructor of the dialog as follows:
     
    CExerciseDlg::CExerciseDlg(CWnd* pParent /*=NULL*/)
    	: CDialog(CExerciseDlg::IDD, pParent)
    {
    	//{{AFX_DATA_INIT(CExerciseDlg)
    	m_Principal = _T("0.00");
    	m_AnnualRate = _T("0");
    	m_NumberOfPeriods = _T("1");
    	m_Compound = 3;
    	m_InterestEarned = _T("0.00");
    	m_AmountEarned = _T("0.00");
    	//}}AFX_DATA_INIT
    	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }
  9. Initiate the BN_CLICKED message of the Calculate button and implement its OnClick() event as follows:
     
    void CExerciseDlg::OnCalculateBtn() 
    {
    	// TODO: Add your control notification handler code here
    	UpdateData();
    	double Principal, AnnualRate, InterestEarned;
    	double FutureValue, RatePerPeriod;
    	int NumberOfPeriods, CompoundType;
    
    	Principal = atof(m_Principal);
    	AnnualRate = atof(m_AnnualRate) / 100;
    
    	switch( m_Compound )
    	{
    	case 0:
    		CompoundType = 12;
    		break;
    	case 1:
    		CompoundType = 4;
    		break;
    	case 2:
    		CompoundType = 2;
    		break;
    	case 3:
    		CompoundType = 1;
    		break;
    	}
    	
    	NumberOfPeriods = atoi(m_NumberOfPeriods);
    	double i = AnnualRate / CompoundType;
    	int n = CompoundType * NumberOfPeriods;
    
    	RatePerPeriod = AnnualRate / NumberOfPeriods;
    	FutureValue = Principal * pow(1 + i, n);
    	InterestEarned = FutureValue - Principal;
    
    	m_InterestEarned.Format("$%.2f", InterestEarned);
    	m_AmountEarned.Format("$%.2f", FutureValue);
    	UpdateData(FALSE);
    }
  10. Test the application
  11. After using it, close it and return to your programming environment.
 

Home Copyright © 2003 FunctionX, Inc.