Summing integers with the for statement. - Java Language Basics

Java examples for Language Basics:for

Description

Summing integers with the for statement.

Demo Code

public class Main 
{
   public static void main(String[] args)
   {//  ww  w. jav a 2 s. c  om
      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);
   } 
}

Result


Related Tutorials