Source of Calculate.java


  1: // Calculate the sum of the integers from 1 to 10 
  2: public class Calculate 
  3: {
  4:    public static void main( String args[] )
  5:    {
  6:       int sum;
  7:       int x;
  8: 
  9:       x = 1;   // initialize x to 1 for counting
 10:       sum = 0; // initialize sum to 0 for totaling
 11: 
 12:       while ( x <= 10 ) // while x is less than or equal to 10      
 13:       {
 14:          sum += x; // add x to sum
 15:          ++x; // increment x
 16:       } // end while
 17: 
 18:       System.out.printf( "The sum is: %d\n", sum );
 19:    } // end main
 20: 
 21: } // end class Calculate
 22: 
 23: 
 24: /**************************************************************************
 25:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 26:  * Pearson Education, Inc. All Rights Reserved.                           *
 27:  *                                                                        *
 28:  * DISCLAIMER: The authors and publisher of this book have used their     *
 29:  * best efforts in preparing the book. These efforts include the          *
 30:  * development, research, and testing of the theories and programs        *
 31:  * to determine their effectiveness. The authors and publisher make       *
 32:  * no warranty of any kind, expressed or implied, with regard to these    *
 33:  * programs or to the documentation contained in these books. The authors *
 34:  * and publisher shall not be liable in any event for incidental or       *
 35:  * consequential damages in connection with, or arising out of, the       *
 36:  * furnishing, performance, or use of these programs.                     *
 37:  *************************************************************************/