Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    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

  • #2
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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 1043-101
    #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();
    	
    }
    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!

  • #3
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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 1043-101
    #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 1043-101 \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==13||months==25||months==37||months==49||months==61||months==73||months==85||months==97||months==109||
    			months==121||months==133||months==145||months==157||months==169||months==181||months==193||months==205||months==217
    			||months==229||months==241||months==253||months==265||months==277||months==289||months==301||months==313||months==325
    			||months==337||months==349)
    		{
    			outfile << "Janurary 1, " << year << fixed << setprecision(2) << "....." << setw(15) << finalAmount << endl;
    			year++;
    		}
    	else
    		if(months==361)
    		{
    			FinalAmount=finalAmount-200;
    			outfile << "Janurary 1, " << year << fixed << setprecision(2) << "....." << setw(15) << FinalAmount << endl;
    		}
    	} while (months<361);
    
    
    	outfile.close();
    	
    }
    just need a counting system for the years if possible? ran out of ideas for the moment. Thanks
    Last edited by m8swezey; 10-23-2009 at 01:50 PM.

  • #4
    Senior Coder TheShaner's Avatar
    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:
    Code:
    if((months % 12) == 0)
    {
        // Print yearly amount
        // Increment year
    }
    You'll increment the year inside the above if statement since you're printing every 12 months.

    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:
    1. Before loop, make principle equal to 200, since this is the initial deposit and set months equal to zero.
    2. You'll make a check using the if statement I explained above. If it passes the check, print the balance and increment the year.
    3. Add the interest to the principle to get a new balance.
    4. Increment months.
    5. 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 (10-25-2009)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •