Computing the number of months to save $1000

  • Context: MHB 
  • Thread starter Thread starter MMOne
  • Start date Start date
  • Tags Tags
    Computing
Click For Summary
SUMMARY

The discussion focuses on calculating the number of months required to save $1000 with a monthly deposit and a 1% interest rate using C++. The original code fails to account for interest earned in the first month, leading to incorrect calculations. A revised version of the code corrects this by calculating initial interest and moving output statements outside the loop for better readability. The final solution accurately computes the total savings and interest earned over the required months.

PREREQUISITES
  • Basic understanding of C++ programming
  • Familiarity with loops and conditional statements in C++
  • Knowledge of floating-point arithmetic and data types
  • Understanding of interest calculations and financial concepts
NEXT STEPS
  • Explore C++ input/output stream manipulation techniques
  • Learn about financial modeling in programming
  • Investigate error handling in C++ for user input
  • Study advanced C++ data structures for financial applications
USEFUL FOR

Beginner programmers, students learning C++, and anyone interested in financial calculations through programming.

MMOne
Messages
3
Reaction score
0
Hello everyone!
I am very new to programming and am beginning to struggle in studies.

I have been working on this same problem for about 5 hours and cannot for the life of me get this thing to compile correctly. Any assistance or guidance for what I should do would be greatly appreciated.

Ask the user for an amount to save each month. Assuming a 1% per month interest rate, how many MONTHS will it take the user to save \$1000. Add the savings at the END of each month.

Code:
#include <iostream>
using namespace std;
int main (){
   
   const double interest = 0.01;
   int deposit = 0;
   int months = 0;
   double totalSaved = 0.0;
   double interestEarned = 0.0;
    cout << "How much do you want to save each month? ";
    cin >> deposit;
      cout << deposit << endl;
   
   while (totalSaved <= 1000){ 
      ++ months;
    
      interestEarned = totalSaved * interest;
      
      cout << "Month " << months << ": $" << totalSaved; 
      
      totalSaved = totalSaved + interestEarned;
      
      cout << " deposited " << deposit <<  " interest earned is " << interestEarned << endl;
          
      if (totalSaved >= 1000){
      cout << "It took " << months << " months, and you now have $" << totalSaved << endl;
   }
   }
   return 0;
}

The outcome doesn't add the interest on the first month but waits for the second and messes up total count calculations.
 
Technology news on Phys.org
Re: Computing the number of months to save \$1000

I think your problem is that you begin your calculations in the loop and don't account for initial month. I rewrote some of your program. It will also be easier to read if you put your cout statements outside of your loop so if your input is very small, it wouldn't produce like a thousand couts for each month.
Code:
#include <iostream>
using namespace std;
int main (){
    
    const double interest = 0.01;
    int deposit = 0;
    int months = 0;
    double totalSaved = 0.0;
    double interestEarned = 0.0;
    cout << "How much do you want to save each month? ";
    cin >> deposit;

    // if the deposit is already 1000 aka no looping required
    if (deposit >= 1000){
    cout << "month: " << months << endl;
    cout << "interest you earned is: " << interestEarned << "$" << endl;
    cout << "now you have: " << totalSaved << "$" << endl;
    }
    // initial interest earning calculations
    interestEarned = deposit * interest;
    totalSaved = deposit + interestEarned;
    // looping for total saved
    while (totalSaved <= 1000){
        interestEarned = totalSaved * interest;
        totalSaved = totalSaved + interestEarned;
        months++;
    }
    // couts for how much you earned in how long
    cout << "month: " << months << endl;
    cout << "interest you earned is: " << interestEarned << "$" << endl;
    cout << "now you have: " << totalSaved << "$" << endl;
    return 0;
}
 
Last edited:

Similar threads

Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
10
Views
2K
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 23 ·
Replies
23
Views
2K