SumIntegers.java

Download
1/*2 * SumIntegers.java3 *4 * Computer Science E-22/S-225 *6 * A class that contains a recursive method for computing the sum of the7 * integers from 1 to n.8 *9 * The main method includes two examples of using this method.10 */11 12public class SumIntegers {13    public static int sum(int n) {14        if (n <= 0) {     // base case15            return 0;16        } else {          // recursive case17            int rest = sum(n - 1);    18            return n + rest;19        }20    }21    22    public static void main(String[] args) {23        int firstSum = sum(3);24        System.out.println("sum(3) = " + firstSum);25        System.out.println("sum(10) = " + sum(10));26    }27}