# Compound interest rate problem

Page 1 of 1

## 4 Replies - 4252 Views - Last Post: 06 October 2009 - 12:36 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=130148&amp;s=c5be1a82c7147a2b1abe0c621943d91f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 baross

Reputation: 0
• Posts: 36
• Joined: 21-May 09

# Compound interest rate problem

Posted 05 October 2009 - 09:30 PM

[quote]
```#include <iostream>
#include <iomanip>
#include <cctype>
#include <cmath>
#include <conio.h>

using namespace std;

{   cout << endl;
cout << "This program demonstrates the benefits of compounding interest."
<< endl;
cout << endl;
cout << "Compare Annual Compounding to:" << endl;
cout << "	M - Monthly Compounding" << endl;
cout << "	W - Weekly Compounding" << endl;
cout << "	D - Daily Compounding" << endl;
cout << "	E - Exit Program" << endl;
cout << endl;
cout << endl;
cout << "Enter choice from Menu above: ";
cin >> Choice;
cout << endl;
Choice = toupper(Choice);
if ((Choice != 'M') && (Choice != 'W') &&
(Choice != 'D') && (Choice != 'E'))
{
cout << "Please enter a choice from above" << endl;
cin >> Choice;
}
if (Choice == 'E')
{
exit(0);
}
return (Choice);
}

double money(double Beginning_Balance)
{
cout << "Enter your beginning balance: ";
cin >> Beginning_Balance;
if (Beginning_Balance < 0)
{
cout << "Please enter positive value" << endl;
cin >> Beginning_Balance;
}
return (Beginning_Balance);
}

double Interest(double& Interest_Rate)
{
cout << "Enter an annual interest rate: ";
cin >> Interest_Rate;
Interest_Rate = Interest_Rate /100;

if (Interest_Rate >.50)
{
cout << "Please enter interest rate lower than 50%" << endl;
cin >> Interest_Rate;

}
Interest_Rate = Interest_Rate * 100;
return (Interest_Rate);
}

int Years(int Term_Years)
{
cout << "Enter a term in whole years: ";
cin >> Term_Years;
if (Term_Years > 100)
{
cout << "Enter term less than 100 years" << endl;
cin >> Term_Years;
}
return (Term_Years);
}

double funcCalculation(char& compounding_choice, double& Start_Balance,
double& interest_rate, int& term_years, double Calc)

{
double principal, result, CalcTotal;
int cDay, cWeek, cMonth, cYear;
const double MONTH=12;
const double Week=52;
const double YEAR=365;
principal = Start_Balance;

Calc = 0;
switch (compounding_choice)
{
case 'M':
interest_rate = interest_rate + 1;
for (cYear = 0; cYear < term_years; cYear++)
{
for (cMonth = 0; cMonth < 12; cMonth++)
{
result = principal * (interest_rate/cMonth);
}
CalcTotal = result;
}
Calc =  principal-CalcTotal;
break;
case 'W':
interest_rate = interest_rate + 1;
for (cYear = 0; cYear < term_years; cYear++)
{
for (cWeek = 0; cWeek < 52; cWeek++)
{
result = principal * (interest_rate/cWeek);
}
CalcTotal = result;
}
break;
case 'D':
interest_rate = interest_rate + 1;
for (cYear = 0; cYear < term_years; cYear++)
{
for (cDay =0; cDay < 365; cDay++)
{
result = principal * (interest_rate/cYear);
}
CalcTotal = result;
}
break;
}
Calc = CalcTotal;
interest_rate = interest_rate -1;
return (Calc);
}

int main()
{
char Choice;
char Compounding, again;
double Start_Balance, rate, Calculation, Final_Calc,Calculate;
int Term_Years;

do
{

double Start_Balance = money(Start_Balance);
double interest_rate = Interest(rate);
int term_years = Years(Term_Years);
double Calculation = funcCalculation(compounding_choice, Start_Balance,
interest_rate, term_years, Calculate);
Final_Calc = Start_Balance + Calculation;

cout << fixed << showpoint << setprecision(2) << endl;
cout << "------------------------------------------------" << endl;
cout << "					 Results					" << endl;
cout << "------------------------------------------------" << endl;
cout << endl;
cout << "Beginning Balance: " << Start_Balance << endl;
cout << "Return after 5 years at " << interest_rate
<< "% interest annually:" << endl;
cout << endl;
cout << "					 Return	   Ending Balance" << endl;
cout << "Compounded Yearly:   \$" << setw(4) << Calculation << setw(4) << "\$" << setw(10)
<< Final_Calc << endl;
cout << "Compounded Weekly:   \$" << setw(4) << Calculation << setw(4) << "\$" << setw(10)
<< Final_Calc << endl;
cout << "------------------------------------------------" << endl;

Choice = toupper(Choice);
} while (Choice != 'E');
return 0;

cout<<endl<<endl;
cout << "Press [enter] to exit" <<endl;
cin.ignore(); //needed because of keyboard input
cin.get();
return 0;
}
```

Is This A Good Question/Topic? 0

## Replies To: Compound interest rate problem

### #2 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

## Re: Compound interest rate problem

Posted 05 October 2009 - 10:18 PM

Do you really expect us to sit here for hours reading your code, pinpoint the problem the post it

### #3 baross

Reputation: 0
• Posts: 36
• Joined: 21-May 09

## Re: Compound interest rate problem

Posted 05 October 2009 - 11:12 PM

ImaSexy, on 5 Oct, 2009 - 09:18 PM, said:

Do you really expect us to sit here for hours reading your code, pinpoint the problem the post it

Quote

The calculation is not calculating correctly! What part of that are you not getting!

### #4 baavgai

• Dreaming Coder

Reputation: 7150
• Posts: 14,890
• Joined: 16-October 07

## Re: Compound interest rate problem

Posted 06 October 2009 - 05:35 AM

First figure out the number of payments, then just use one loop.

```int getNumberOfPayments(char compounding_choice, int term_years) {
if (compounding_choice=='M') { return term_years * 12; }
if (compounding_choice=='W') { return term_years * 52; }
// daily
return term_years * 365;
}

```

Also, for this, doubles can do strange things. Work in ints and pennies for better results.

### #5 baross

Reputation: 0
• Posts: 36
• Joined: 21-May 09

## Re: Compound interest rate problem

Posted 06 October 2009 - 12:36 PM

Quote

baavgai, on 6 Oct, 2009 - 04:35 AM, said:

First figure out the number of payments, then just use one loop.

```int getNumberOfPayments(char compounding_choice, int term_years) {
if (compounding_choice=='M') { return term_years * 12; }
if (compounding_choice=='W') { return term_years * 52; }
// daily
return term_years * 365;
}

```

Also, for this, doubles can do strange things. Work in ints and pennies for better results.

quote]Thank you
[