1

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..."

3
  • I'm assuming you're using '5' as input, yes? What output are you getting? Commented Mar 31, 2013 at 8:31
  • With an input of 5 I get "5 + 44 + 33 + 22 + 11Exception in thread "main" java.lang.StackOverflowError" Commented Mar 31, 2013 at 8:33
  • Your recursive call is inconditional and that leads to a loop. Commented Mar 31, 2013 at 8:33

8 Answers 8

1

You're missing the termination condition. Try this:

public static void sumReverse(int n)
{
    if(n == 1) {
        System.out.print(n);
        return;
    }
    else if(n > 0)
    {
        System.out.print(n + " + " + (n-1));
    } else return;
    sumReverse(--n);
}

This function will stop once n hits 1 or if 1 is lower or equal than zero.

An alternative would be:

public static void sumReverse(int n)
{
    if(n == 1) System.out.print(n);
    else if(n > 0)
    {
        System.out.print(n + " + " + (n-1));
        sumReverse(--n);
    }        
}

This has the same effect.

Sign up to request clarification or add additional context in comments.

1 Comment

No, see the second function for an alternative. Also, you may want to remove the (n - 1), because it will lead to the 5 + 44 effect.
0

You need to recurse only in the good case. Here you recurse everytime so you get an infinite loop.

    else if(x > 0)
    {
        System.out.print(x + " + ");
        x--;
        sumReverse(x);
    }

Notice I also deleted the + (x - 1) as it will be printed in the next recursion.

2 Comments

By the way, is recursion a requirement? If not, a simple for (int x = n; x > 0; x--) would do the trick in a cleaner way
Yeah, recursion was a requirement or I would have just used a loop.
0

As you don't accumulate the sum, you can't compute it. The best would be to return it : to define a function summing until n and call it recursively.

I'll give it to you in pseudo-code as I understand you'd prefer to learn rather than to have your work done :

function sum(int n) -> int {
     if x==1 : return 1
     else : return n + sum(n-1)
}

Then you would print sum(n).

Comments

0
public static void sumReverse(int n){
    if(n==0)
        return;

    if(n == 1){
        System.out.print(n);
    }else if(n > 0)
    {
        System.out.print(n + " + ");
        sumReverse(n-1);
    }

}

Comments

0
  • You are not stopping the recursion.
  • You should add return after the recursion stop condition.
  • Another thing, remove the + (n-1) from the print in the else and you should be fine.
  • Plus, it's redundant to assign n to x. You can work directly on n.

Try to draw the recursion calls on paper and you'll better understand how it works.

public static void sumReverse(int n)
{
     if(n == 1) {
         System.out.print(n);
         return;
     }
     else if(n > 0)
     {
         System.out.print(n + " + ");
     }
     sumReverse(n-1);
}

Comments

0

Would like to point out a few things: 1. This problem is not a usual candidate for recursion. 2. Your best bet if you intend to use recursion is to hold the return values as String and print finally in your main method as below. 3. Declaring a new variable x is really unnecessary.

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();
        System.out.println(sumReverse(input));
    }
    public static String sumReverse(int n)
    {
        String a = "";
        if(n > 1)
        {
            return (n + "+" + sumReverse(n-1));
        }
        return "1";
    }

Comments

0
public class Main {

    public static String s = "";

    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();
        String b=sumReverse(input);
        System.out.println(b);
    }

    public static String sumReverse(int n) {
        int x = n;

         if (x == 1) {
          s = "1" + s;
          return s;
        } else if (x > 0) {
           s ="+"+  Integer.toString(x) +s ;
           sumReverse(x - 1);
    }
        return s;

    }
}

The Output:

Please enter an integer greater than one: 
10 
1+2+3+4+5+6+7+8+9+10

Comments

0
public static void sum(int n){
  sumReverse(n-1);
  System.out.print(n);
}

public static int sumReverse(int n){
  if(n==1){
    return n;
  }
  System.out.print(sumReverse(n-1)+"+");
  return n;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.