For Advertisements on this blog Contact Us at developgram@gmail.com or +91-9923438816

Simple Programming Tutorials For Beginners

Wednesday, April 19, 2017

First Five Even Numbers Addition

<html>
<body>
  <script>

    var sum = 0;

    for (i = 0; i <= 8; i++)
    {
      if (i % 2 == 0)
        sum = parseInt(sum + i);
    }

    document.write("Sum of first five even numbers is " + sum);

    //It will only add first five even numbers independent of range of loop
    /*var sum = 0, count = 0;

    for (i = 0; i <= 80; i++)
    {
      if (count < 5)
      {
        if (i % 2 == 0)
        {
          ++count;
          sum = parseInt(sum + i);
        }
      }

      else
        break;
    }

    document.write("Sum of first five even numbers is " + sum);
    */
  </script>
</body>
</html>


No comments:

Post a Comment

SEARCH THIS BLOG