Calculates and prints the sum of the integers from 1 to 10. - Java Language Basics

Java examples for Language Basics:while

Description

Calculates and prints the sum of the integers from 1 to 10.

Demo Code

public class Main 
{
   public static void main(String[] args)
   {/*from   w w w .j a  v a2  s. c om*/
      int sum = 0;
      int x = 1;

      while (x <= 10) // while x is less than or equal to 10
      {
         sum += x; // add x to sum
         ++x; // increment x
      } 

      System.out.printf("The sum is: %d%n", sum);
   } 
}

Result


Related Tutorials