C++ Lesson 1 - How to write a compound interest calculatorsteemCreated with Sketch.

in #programming9 years ago (edited)

Latest version of compound interest calc

C++ Lesson

Note: I am not a programmer by trade. I am merely fluent in C++ well enough to read, write, and make casual use of it. If you are interested in analyzing more complicated programs then these lessons may not be for you as these are programs I wrote myself in my spare time for fun and to educate.

Compound Interest Calculator

The Compound Interest Calculator is a program I wrote to automate a process of outputting the compound interest I could gain off different investments. I did not write this app because it is particularly useful but merely out of bordem to brush up on my skills. In this article I've decided to take the opportunity to share this simple app code so that it can educate others, and so we as a community can improve on it and discuss the improvements.

   #include <cmath>
   #include <iostream>
    
    
    
    using namespace std;
    // USING GLOBAL VARIABLES TO MAKE PROGRAM MORE EXTENSIBLE
        float a; //amount
        float principal;
        float rate;
        int month; //time is divided into 12 months to represent a year
        
      
    int main ()
    {
    
        cout << "How much money do you have to start?" << endl;
        cin >> principal;
        cout << "Your principal amount is:  ";
        cout << principal << endl;
        cout << "What yearly interest rate percentage will you have?" << endl;
        cin >> rate;
        cout << "your rate is:  ";
        cout << rate << endl;
        cout << "We now calculate your yearly investment growth over time" << endl;
    
    
    
    // a for loop which applies the formula to calculate yearly APR
        for(int month = 1; month <= 12; month++) {
            a = principal * pow (1 + rate/12, month);
        cout << a << endl;
    
        if (a >= 1000) {
        cout << "Wow!" << endl;
    
        }
        }
    
    
    }`

Analysis and discussion of the code.

cout << "How much money do you have to start?" << endl;
    cin >> principal;
    cout << "Your principal amount is:  ";
    cout << principal << endl;
    cout << "What yearly interest rate percentage will you have?" << endl;
    cin >> rate;
    cout << "your rate is:  ";
    cout << rate << endl;
    cout << "We now calculate your investment growth over 12 months" << endl;

Beginners in C++ are aware of cin and cout which represent the character input and character output functions. Cin takes input from the user via the console and stores it in the variable "principal". Cout prints a message to the user requesting the desired input for the calculation to take place.

In our code we also utilize cin to store the rate. Rate is of course a float, and principal is also an float.

using namespace std;
// USING GLOBAL VARIABLES TO MAKE PROGRAM MORE EXTENSIBLE
    float a;
    float principal;
    float rate;
    int month;

The global variables are declared and it's global because we can reuse these variables if we choose to extend the program in the future. We have month which must be an integer. Rate and principal are necessarily floats. The difference is with percentages you want to use floats, and with principal you can get more precision if you use a float. This variable structure allows you to follow the flow of the program from input functions, which get stored in these variables as integers or floats, and from there a calculation is made using a loop.

    for(int month = 1; month <= 12; month++) {
        a = principal * pow (1 + rate/12, month);
    cout << a << endl;

    if (a >= 1000) {
    cout << "Wow!" << endl;

    }
    }


}

In this last block of code we have a for loop. This for loop utilizes the data contained in month, where a month must be between 1 and 12. In computer speech the operator <= means less than or equal to 12. The loop starts at 1, and iterates by adding 1 until it reaches 12 and resets. Variable a represents the principal and the main formula is a = principal * pow(1+rate, month); .

Since the result is contained in a, the if a >=1000 simply sets a trigger where if the amount surpasses the treshold of 1000 the output will say "Wow". You can set this to anything.

What can we do to extend this program? We can make the alert configurable. Here is how we do it:

First you add the alert int variable as the container for your configuration.

using namespace std;
// USING GLOBAL VARIABLES TO MAKE PROGRAM MORE EXTENSIBLE
    float a; //amount
    float principal;
    float rate;
    int month;
    int alert; 

Next you send an output to the user asking them to configure the alert treshold using cout.
And then you store their integer in cin >> alert.

int main ()
{

    cout << "How much money do you have to start?" << endl;
    cin >> principal;
    cout << "Your principal amount is:  ";
    cout << principal << endl;
    cout << "What yearly interest rate percentage will you have?" << endl;
    cin >> rate;
    cout << "Your rate is:  ";
    cout << rate << endl;
    cout << "Please configure your alert threshold now:" << endl;
    cin >> alert;
    cout << "We now calculate your yearly investment growth over time" << endl;

Finally revisit the for loop and modify it like this:

   if (a >= alert) {
    cout << "Wow!" << endl;

Have fun learning C++. More analysis and tutorials will be coming in the future for C++ and other languages.

Sort:  

Thanks for sharing. I'm not a professional programmer too, but prefer scripting languages like a Python for fun.

I'll be covering Python.

Coin Marketplace

STEEM 0.12
TRX 0.34
JST 0.032
BTC 110045.02
ETH 4022.71
USDT 1.00
SBD 0.76