C++ Tutorial #3 — Order of operations, Simple interest, compounding interest


Topics covered: Order of operations in C++, simple interest formula / calculation, compounding interest formula calculation

Source code:

#include 
using namespace std;   
 
 
int main(){
    
    //PEMDAS
    //In math -- Parenthesis, Exponents, Mult / division, add / sub
    //In C++ Paren. -- Evaluated first
    //Mult / Div 
    //Add / Sub -- Last in priority
    //Evaluates left to right
    
    //1*5-4*3
    //5-12
    double principal=0, rate=0, time=0;
    
    cout << "What is the initial investment?  ";
    cin >> principal;
    
    cout << "What is annual percentage yield (APY -- APR)?  ";
    cin >> rate;
    
    cout << "How many years will this be invested for? ";
    cin >> time;
    
    
    cout << "The amount of money at the end of the time period will be: ";
    
     return (0);       
}


Order of operations
In C++, there is an order of operations, much like the one that we see in traditional mathematics. In C++, the order of operations is as follows:

  1. Parenthesis
  2. Multiplication and Division
  3. Addition and subtraction.
  4. “Left to right”


Cmath library
The Cmath library contains a lot of useful things, for instance square root (sqrt) floating point modulo (fmod / fmodf) and more. In this case we are using ‘pow’ which is used like this: pow(double, int);