Computer Science 202
Introduction to Programming

Fall 2012, The College of Saint Rose

Sum1To10 BlueJ Project

Click here to download a BlueJ project for Sum1To10.


Download file: Sum1To10.vls


Sum1To10 Source Code

The Java source code for Sum1To10 is below. Click on a file name to download it.


Sum1To10.java

/*
 * Example Sum1To10: the use of a simple for loop to compute
 * the sum of the numbers 1 to 10.
 *
 * Jim Teresco, The College of Saint Rose, CSC 202, Fall 2012
 *
 * $Id: Sum1To10.java 1937 2012-10-08 18:06:13Z terescoj $
 */

import javax.swing.JOptionPane;

public class Sum1To10 {

    public static void main(String[] args) {
	
        // initialize a variable for our running sum
        int sum = 0;
        
        // now have a for loop that counts 1-10, adding the value
        // of the counter to the sum inside each iteration of the loop
        for (int number = 1; number <= 10; number++) {
            
            sum += number;
        }
        
        // report the sum
        JOptionPane.showMessageDialog(null, "Our sum is " + sum);
    }
}