// Helene Martin, CSE 142 // Demonstrates cumulative algorithms. public class CumulativeSum { public static void main(String[] args) { // add up all numbers from 1 - 1000 // int sum = 1 + 2 + 3... I DON'T HAVE ALL DAY int theSum = sum(1000); System.out.println("The sum: " + theSum); System.out.println("2 ^ 5 = " + pow(2, 5)); } // cumulative product algorithm // calculates and returns base raised to some exponent public static int pow(int base, int exponent) { int product = 1; for (int i = 1; i <= exponent; i++) { product = base * product; } return product; } // cumulative sum algorithm // calculates and returns the sum from 1 up to some maximum public static int sum(int max) { int sum = 0; for (int i = 1; i <= max; i++) { sum = sum + i; } return sum; } }