Calculate compound interest using function - C++ Function

C++ examples for Function:Function Creation

Description

Calculate compound interest using function

Demo Code

#include <iostream> 
#include <cmath> 
using namespace std ; 

double compoundInterest ( double P, double i, int n) { 
    double interest = P * pow (1 + 0.01*i, n) - P; 
    return interest ; 
} 

int main () { // ww  w.  j ava2 s  .  com
    int principal ; 
    double interestRate ; 
    int numberOfYears ; 
    cout << " How much are you investing ?\n"; 
    cin >> principal ; 
    cout << " What 's the annual interest rate (%)?\ n"; 
    cin >> interestRate ; 
    cout << " How long for ( years )?\ n"; 
    cin >> numberOfYears ; 
    double interest = compoundInterest ( principal , interestRate , numberOfYears ); 
    cout << " You will earn "; 
    cout << interest ; 
    cout << "\n"; 
    return 0; 
} 

Related Tutorials