# Figuring Compound Interest

Page 1 of 1

## 7 Replies - 1494 Views - Last Post: 03 February 2010 - 08:23 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=153777&amp;s=068120a5189b731c69b7dd156483fee6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 GCheek

Reputation: 6
• Posts: 92
• Joined: 31-January 10

# Figuring Compound Interest

Posted 03 February 2010 - 06:54 PM

First off let me thank you all for your help and guidance. I have been learning so much from you all as well as my college computer science class. I cant wait for the day that I can help others the way that you have helped me.

Ok so the second part of my homework for today is this.
// Write a compound-interest calculator. It should read in the principal
// amount (initial investment), the annual nominal interest rate, the
// number of times the interest is compounded per year and the number of
// years invested. It should then output the final amount to the nearest
// cent.
// Your input and output should look like this:
//
// Enter the principal amount in dollars: 1000
// Enter the annual nominal interest rate: 3.5
// Enter the number of times the interest is compounded per year: 12
// Enter the number of years invested: 3
//
// The final amount is \$1110.54.
Given those facts I wrote my program and used those numbers to test my math, needless to say I didn't get the same result. After several hours of toiling I decided to ask for help. No matter how I work the equation for compound interest I cant replicate the results. So without further ado here is my code to this point

``` int principal_amount, compound_num, years_invested;
double annual_interest, final_amount,alpha_interest,alpha, bravo, charlie;

cout << "Please enter the principal amount in dollars: ";
cin >> principal_amount;
cout << "Please enter the annual nominal interest rate: ";
cin >> alpha_interest;
cout << "Please enter the number of time the interest is compounded per year: ";
cin >> compound_num;
cout << "Please enter the number of years invested: ";
cin >> years_invested;

cout << showpoint;
cout << setprecision(5);

annual_interest = alpha_interest / 100;
cout << "\n" <<annual_interest<< "\n";

alpha = compound_num / annual_interest;
bravo = alpha + 1;
charlie = compound_num * years_invested;
final_amount = principal_amount * bravo * charlie;

cout << "\n The Final Amount Is: \$"<<final_amount<< "\n";

// A zero return value indicates that the program finished without error.
return 0;
}

```

Please look through this code and let me know what I can do to resolve this.

Is This A Good Question/Topic? 0

## Replies To: Figuring Compound Interest

### #2 KYA

• yay verily

Reputation: 3155
• Posts: 19,198
• Joined: 14-September 07

## Re: Figuring Compound Interest

Posted 03 February 2010 - 07:19 PM

This is the formula you want to use:

so in terms of your variables:

```final_amount = principal_amount*pow((1+(annual_interest/compound_num)), compound_num*years_invested);

```

I got 1110.54, example test:

```int main(){
double final_amount, principal_amount, annual_interest,
compound_num, years_invested;

principal_amount = 1000.00;
annual_interest = .035;
compound_num = 12;
years_invested = 3;

final_amount = principal_amount*pow((1+(annual_interest/compound_num)), compound_num*years_invested);

cout << final_amount << endl;
return 0;
}

```

### #3 GCheek

Reputation: 6
• Posts: 92
• Joined: 31-January 10

## Re: Figuring Compound Interest

Posted 03 February 2010 - 07:25 PM

ok so I need to go back over the POW concept I see that I completely missed that, and I'm assuming that if I configure the variables for user input this will still work. Anyway thanks for nudging me in the right direction you rock man.

### #4 GCheek

Reputation: 6
• Posts: 92
• Joined: 31-January 10

## Re: Figuring Compound Interest

Posted 03 February 2010 - 07:36 PM

ok so as far as POW this is just telling the program to use exponents, so my question is how in this situation does the program know which exponents to multiply by?

### #5 KYA

• yay verily

Reputation: 3155
• Posts: 19,198
• Joined: 14-September 07

## Re: Figuring Compound Interest

Posted 03 February 2010 - 07:41 PM

pow is:

```pow(number, exponent);

```

### #6 GCheek

Reputation: 6
• Posts: 92
• Joined: 31-January 10

## Re: Figuring Compound Interest

Posted 03 February 2010 - 08:05 PM

awesome I completely understand this now i just didnt see where you had defined it in your code but I see that you quite literally composed the equation in your code once I looked closely. Nicely done and thanks again your a scholar and a gentleman lol.

### #7 KYA

• yay verily

Reputation: 3155
• Posts: 19,198
• Joined: 14-September 07

## Re: Figuring Compound Interest

Posted 03 February 2010 - 08:16 PM

Yeah sorry about that, it's from one of the standard libraries.

### #8 GCheek

Reputation: 6
• Posts: 92
• Joined: 31-January 10

## Re: Figuring Compound Interest

Posted 03 February 2010 - 08:23 PM

It's all good, and I know its a rarity but I tend to learn more by picking a code apart and asking why, why, why. I have a few people in my class that have been programming for a year or more and they don't understand why they have to write things certain ways, to them its just a grade, to me this isn't just getting a degree its learning a trade. So I appreciate you taking time out of your busy day to help me. Thank you, thank you , thank you.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }