Java for loop sum even numbers between 2 and 20

Question

We would like to use for loop to sum even numbers between 2 and 20

// Summing integers with the for statement.
public class Main 
{
   public static void main(String[] args)
   {//ww  w  .  ja va  2  s . c  om
      int total = 0;

      // total even integers from 2 through 20
      //your code here

      System.out.printf("Sum is %d%n", total);
   } 
} 


// Summing integers with the for statement.

public class Main 
{
   public static void main(String[] args)
   {
      int total = 0;

      // total even integers from 2 through 20
      for (int number = 2; number <= 20; number += 2)
         total += number;

      System.out.printf("Sum is %d%n", total);
   } 
} 



PreviousNext

Related