title: CodeWars - Sum of odd Numbers - For loop
body: <p>I have attempted to make a piece of code that accepts an input of "n", calculates the sum of the numbers on the nth line an odd number triangle, which looks like:</p>
  
  <pre><code>             1
            3     5
         7     9    11
     13    15    17    19
  21    23    25    27    29
  </code></pre>
  
  <p>etc. So for <code>n = 3</code>, the sum would be <code>7 + 9 + 11</code> ie <code>27</code></p>
  
  <p>I know that n is not only the line number but also equals the number of numbers on that line. so <code>n = 3</code> also have 3 odd numbers on it. Therefore I thought that I could get the first number of the line, then just loop through adding two to the previous number then sum it. </p>
  
  <p>My code below doesn't work, so for an input of <code>n=43</code>, my code calculates that the sum is <code>3570</code> whereas it actually equals <code>79507</code>.</p>
  
  <pre><code>public static int rowSumOddNumbers(int n) {
      int firstNum = (2 * n) - 1;
      int total = 0;
      for (int i = 0; i &lt; n; i++) {
          total += (firstNum + 2);
      }
      return total;
  }
  </code></pre>
  
  <p>I believe my problem is that I'm not adding the previous number together with the current number + 2. Should it be that I need to store the previous loop's outcome than add it to the current loop's outcome? </p>
  
  <p>Any help appreciated.</p>