Figuring Compound Interest

Can't get the math right

Page 1 of 1

7 Replies - 1267 Views - Last Post: 03 February 2010 - 08:23 PM Rate Topic: -----

#1 GCheek  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • 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  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Figuring Compound Interest

Posted 03 February 2010 - 07:19 PM

This is the formula you want to use:

Posted Image

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;
}


Was This Post Helpful? 1
  • +
  • -

#3 GCheek  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 GCheek  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Figuring Compound Interest

Posted 03 February 2010 - 07:41 PM

pow is:

pow(number, exponent);


Was This Post Helpful? 1
  • +
  • -

#6 GCheek  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • 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.
Was This Post Helpful? 1
  • +
  • -

#8 GCheek  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1