I am trying to write a simple recursion program that will print out the canonical sum of all of the integers preceding the input and the input itself. For example, entering 5 should print out "1 + 2 + 3 + 4 + 5". The input must be greater than zero. A bump in the right direction would be appreciated.
import java.util.Scanner;
public class Quiz10
{
public static void main (String[] args)
{
int input;
System.out.println("Please enter an integer greater than one: ");
Scanner scan = new Scanner(System.in);
input = scan.nextInt();
sumReverse(input);
}
public static void sumReverse(int n)
{
int x = n;
if(x == 1)
System.out.print(x);
else if(x > 0)
{
System.out.print(x + " + " + (x-1));
}
x--;
sumReverse(x);
}
}
Edit: with an input of 5 I am currently getting: "5 + 44 + 33 + 22 + 11Exception in thread "main" java.lang.StackOverflowError..."