Interest Calculator

Recently I found myself calculating interest and interest duration, I heaven’t found any decent tool for this so I did one by myself in C++. It does nothing else but prints the numbers you were looking for right in your terminal window.

#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;

class Interest_C
{
private:

float round (float value)
{
return ((int)(value * 100 + .5) / 100.0);
}

public:

float FinalSum(float isum, float percent, float deposit, int period)
{
float SUM = isum;
int i = 0;

do {
SUM = SUM + deposit + ((SUM * percent / 100))/12; because we the interest is for a whole year
cout << i+1 << “. month” << endl;
cout.setf(ios::fixed);
cout << setprecision(0) << round(SUM) << endl;
cout << “Interest = ” << round(((SUM * percent / 100))/12) << endl;
cout << endl;
i++;
}
while (i != period);

return SUM;
}

int FinalPeriod(float isum, float percent, float deposit, float fsum)
{
float SUM = isum;
int i = 0;
do {
SUM = SUM + deposit + ((SUM * percent / 100))/12; //we have to divide by 12 because we the interest is for a whole year
cout << i+1 << “. month” << endl;
cout.setf(ios::fixed);
cout << setprecision(0) << round(SUM) << endl;
cout << “Interest = ” << round(((SUM * percent / 100))/12) << endl;
cout << endl;
i++;
}
while (SUM <= fsum);
return SUM;
}

};

struct myseps : numpunct<char> {
char do_thousands_sep() const { return ‘,’; }
string do_grouping() const { return “\3”; }
};

int main()

{
float initial, p, dep, sum;
int period;
Interest_C *Interest = new Interest_C();
cout.imbue(locale(locale(), new myseps));

char menu = 0;

while (menu != 1)
{
cout << “1 – Find out the sum” <<endl;
cout << “2 – Find out the period” << endl;
cout << “0 – exit” << endl;
char menu;
cout << “1 or 2? “;
cin >> menu;
getchar();

switch (menu)
{
case ‘1’:
cout <<“Initial amount: ” ; cin >> initial;
cout <<“Interest (%): “; cin >> p;
cout <<“Monthly deposit: “; cin >> dep;
cout <<“Period (months): “; cin >> period;
if (period <= 0) period = 1;
Interest->FinalSum(initial, p, dep, period);
break;

case ‘2’:
cout <<“Initial amount: ” ; cin >> initial;
cout <<“Interest (%): “; cin >> p;
cout <<“Monthly deposit: “; cin >> dep;
cout <<“Final sum: “; cin >> sum;
Interest->FinalPeriod(initial, p, dep, sum);
break;

case ‘0’:
return 0;
break;
}
}

delete Interest;
return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.