Java Loops II

  • + 1 comment

    can u explain this code

    • + 0 comments

      Hi. We want to print each term in the formula provided in the problem description. This is the code that does so:

      for (int j = 0; j < n; j++) {
           a += b * (int) Math.pow(2, j);
           System.out.print(a + " ");
      }
      

      Try to compare the value of a in my code above to each term in the equation in the problem statement. First, for j= 0, we will get:

      a + (2^0)*b
      

      which matches the first term in the formula in the problem statement.

      Try to see what we get for j=1 and j=2.

      HackerRank solutions.