Chapter 8: mod sum


January 30th, 2015

Share your recursive solution to yesterday’s assigment

Modify the method that calculates the sum of the integers between 1 and N shown in this chapter. Have the new version match the following recursive definition: The sum of 1 to N is the sum of 1 to (N/2) plus the sum of (N/2 + 1) to N. Trace your solution using an N of 7.

// This method returns the sum of 1 to num
public int sum (int num) 
{
   int result; 
   if (num == 1)
       result = 1;
   else
       result = num + sum (num-1); 
   return result;
}