Enjoy an ad free experience by logging in. Not a member yet? Register.


Results 1 to 4 of 4
Thread: C++ ? Compound Monthly interest

10232009, 10:00 AM #1
 Join Date
 Oct 2009
 Posts
 3
 Thanks
 1
 Thanked 0 Times in 0 Posts
C++ ? Compound Monthly interest
Hello, I just have a few questions about how to do this assignmnet.
I have the forumla for it already, just can't seem to wrap my mind around how I'm going to printout the Yearly total each year.
For 30 years,annual interest at .05, compounded monthly, depositing 200 a month, printout the total amount each year at jan 1.
I have my idea on how to write out the basic code, but what throws it down the drain is how i have to printout Jan 1 20XX each year for the total amount and still add 200 a month.
what would be better, a do while loop or just a plain while loop?
Not asking anyone to do it for me, i just need to get my mind a jump start so I can figure out the rest on my own.
thanks
10232009, 10:41 AM
#2
 Join Date
 Oct 2009
 Posts
 3
 Thanks
 1
 Thanked 0 Times in 0 Posts
this is my basic, raw code that ive written up so far. just need a way to print out the Jan 1 of every year amount: not all 360 months which is what i have. Ill continue to work on it, but much help is welcome!Code://This program will calculate Hulk Hogan's investment in a Roth IRA at //5% interest annualy compounded monthly at the first of each month for 30 years or 360 months. //Using the formual M=P(1+I)^N where I is .05/12 and N is the number of months (360). Deposits $200 //each month starting Jan 1 2010 ending Dec 1 2039. //Wrote by Matt Swezey CMPS 1043101 #include <fstream> #include<cmath> #include<iomanip> using namespace std; void main() { ofstream outfile; ifstream infile; outfile.open("yearlyamount.txt"); double finalAmount; double principal; double intperyr; int years; int months; principal=200; months=0; intperyr=.00416666667; do { finalAmount=principal*pow((1+intperyr),12); principal=finalAmount+200; outfile << fixed << setprecision(2) << principal << endl; months++; } while (months<360); outfile.close(); }
10232009, 11:28 AM
#3
 Join Date
 Oct 2009
 Posts
 3
 Thanks
 1
 Thanked 0 Times in 0 Posts
just need a counting system for the years if possible? ran out of ideas for the moment. ThanksCode://This program will calculate Hulk Hogan's investment in a Roth IRA at //5% interest annualy compounded monthly at the first of each month for 30 years or 360 months. //Using the formual M=P(1+I)^N where I is .05/12 and N is the number of months (360). Deposits $200 //each month starting Jan 1 2010 ending Dec 1 2039. //Wrote by Matt Swezey CMPS 1043101 #include <fstream> #include<cmath> #include<iomanip> using namespace std; void main() { ofstream outfile; ifstream infile; outfile.open("yearlyamount.txt"); double finalAmount; double principal; double interestPerMonth; int year; int months; double FinalAmount; principal=0; months=0; interestPerMonth=.00416666667; year=2010; outfile << "Matt Swezey \n"; outfile << "CMPS 1043101 \n"; outfile << "Due: October 28, 2009\n\n\n\n\n"; do { finalAmount=principal*pow((1+interestPerMonth),12); principal=finalAmount+200; finalAmount=principal; months++; if (months==1) { outfile << "Janurary 1, " << year << fixed << setprecision(2) << "....." << setw(15) << finalAmount << " *First Deposit*" << endl; year++; } else if(months==13months==25months==37months==49months==61months==73months==85months==97months==109 months==121months==133months==145months==157months==169months==181months==193months==205months==217 months==229months==241months==253months==265months==277months==289months==301months==313months==325 months==337months==349) { outfile << "Janurary 1, " << year << fixed << setprecision(2) << "....." << setw(15) << finalAmount << endl; year++; } else if(months==361) { FinalAmount=finalAmount200; outfile << "Janurary 1, " << year << fixed << setprecision(2) << "....." << setw(15) << FinalAmount << endl; } } while (months<361); outfile.close(); }
Last edited by m8swezey; 10232009 at 12:50 PM.
10232009, 03:03 PM
#4
 Join Date
 Sep 2005
 Location
 Orlando, FL
 Posts
 1,126
 Thanks
 2
 Thanked 40 Times in 40 Posts
First, get rid of all those if statements and replace it with the one I'll explain below.
Using your months counter, you want to check every 12 months. To check when to print, you'll divide the months counter by 12. If you have a remainder of zero (meaning the count is divisible by 12, i.e. 12, 24, 36, 48, etc), you'll print; else you won't.
To find the remainder, you must use the modulus operator. In C++ and many other languages, the modulus operator is the percent (%) sign. So you'll do:
You'll increment the year inside the above if statement since you're printing every 12 months.Code:if((months % 12) == 0) { // Print yearly amount // Increment year }
Now, to prevent having to do that last check to see if you've gone to 361 months, you should move your months increment to the very bottom of the loop before the while check. January will start out as month zero then and it'll print that month. 12 months from then will be the following January since the months counter will be at 12 and will print.
You'll also need to calculate the interest and add it to the principle at the end of the loop after you've done your 12 month check. This is because the there is no interest to add in the first month. Your first month's print out should be the initial deposit, $200.
Since your first month is month 0, your while loop will be while(months < 360), since starting at month 0 and going to month 359 is a total of 360 months.
Thus, the process will be:
 Before loop, make principle equal to 200, since this is the initial deposit and set months equal to zero.
 You'll make a check using the if statement I explained above. If it passes the check, print the balance and increment the year.
 Add the interest to the principle to get a new balance.
 Increment months.
 The while loop will check if it's still less than 360. If so, loop. If not, exit loop and do one final print statement of the balance.
You were pretty close. You just need a little rearrangement and better efficiency to check on which months to print.
Good luck!
Shane
Users who have thanked TheShaner for this post:
m8swezey (10252009)