title: Getting negative values while incrementing numbers in a while loop
body: <p>I am trying to create a program that will calculate how many days I will need to save X amount of money if I increment the daily contribution by the previous and initial values every day.</p>
  <p>Eg: my starting contribution on day 1 is 2, then on day 2 it will be 4, day 3 it will be 6 etc, so the total money in the pot for 3 days will be 12.</p>
  <p>My program at the moment looks like this:</p>
  <pre><code>    int startingValue = 2;
      int dailyAmount = startingValue;
      int pot = 0;
      int desiredMoney = 43554;
      int daysSaved =0;
  
  
      while (pot != desiredMoney){
  
          pot += dailyAmount;
          dailyAmount += startingValue;
          daysSaved++;
      }
  
      System.out.println(&quot;Daily amount needed : &quot; + dailyAmount + &quot; Days saved : &quot; + daysSaved + &quot; Money in the pot : &quot; + pot);
  </code></pre>
  <p>Current output of this program is this :</p>
  <pre><code>Daily amount needed : 294923972 Days saved : 147461985 Money in the pot : 43554
  </code></pre>
  <p>which doesn't look quite right, but I am not sure why. When I run it in debug mode, it makes sense for the first couple of runs, but when I put the <code>sout</code> statement inside the <code>while</code> loop after a while I see negative numbers such as these:</p>
  <pre><code>Daily amount needed : 1054054 Days saved : 527026 Money in the pot : -1415942538
  </code></pre>
  <p>I would expect to see <code>daysSaved</code> as around 1000 days, but my program is clearly broken. Will appreciate any help!</p>