Advertisement
Guest User

Recursive Sum of Numbers help

a guest
May 20th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.68 KB | None | 0 0
  1. public class SumOfNumbers {
  2.  
  3.     /**
  4.      * @param args the command line arguments
  5.      */
  6.     public static void main(String[] args) {
  7.         // TODO code application logic here
  8.        
  9.         int last = 5;
  10.         int start = 1;
  11.         int current = 0;
  12.        
  13.         System.out.println("Final: "+ sumOfAll(start, last, current));
  14.     }
  15.    
  16.     public static int sumOfAll(int start, int last, int current)
  17.     {
  18.         int total = current;
  19.         if (start <= last)
  20.         {
  21.             total += start;
  22.             start++;
  23.             sumOfAll(start, last, total);
  24.             System.out.println(total);
  25.         }
  26.        
  27.         return total;
  28.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement