1

This code below allows the user to input values and then takes the sum of all values given and then gives the sum by recursion. The code below allows the user to input each int and provides sum

import java.util.Scanner;

public class Recursion {
    
    public static int Recursion1(int userSum) 
    {
        
        if (userSum == 0) {
            return userSum;
            } else {
            return userSum + Recursion1(userSum - 1);
            }

    }       
    
    public static void main(String[] args) 
    {
        int userSum = 0;
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Program Started");
        
        System.out.println(Recursion1(userSum));
    
        int counter = 0;
        int i;
        for(i=0;i < 5;i++) {
            //ask user input 
             System.out.print("Enter any number: ");
             userSum = scan.nextInt();
        }
          scan.close();
          int sumNum=Recursion1(userSum);
          System.out.println("The sum of digits is: "+sumNum);
          System.out.println("Scanner Closed.");
    }

}

5
  • For example when I add the sum for 10 10 10 10 10 it equals 55, which isn't correct. It only seems to work if I calculate the sum of 1 2 3 4 5 which is 15. Commented Dec 18, 2021 at 3:22
  • what is your expected ans for 10 10 10 10 10? @Tonia Saba Commented Dec 18, 2021 at 3:26
  • you are only taking the last userInput, and then adding all the numbers up to it. So if the last input is 8 you are actually doing 8+7+6+5+4+3+2+1. That is why doing 1,2,3,4,5 seems like it is working, since the last one is 5 it does 5+4+3+2+1 Commented Dec 18, 2021 at 3:27
  • @WingKuiTsoi 50, instead of 55 Commented Dec 18, 2021 at 3:27
  • Recursion1 should be recursion1 according to java naming conventions. Commented Dec 18, 2021 at 6:16

2 Answers 2

2

There are several issues with your code, for starters:

 for(i=0;i < 5;i++) {
        //ask user input 
         System.out.print("Enter any number: ");
         userSum = scan.nextInt();
    }

In the above code, you're not storing the 5 numbers inserted by the user, you're only storing the last one because you keep overwriting the variable userSum.

To store several values, the best think would be either an array or a List.

The second issue with your code is your recursive method:

 public static int Recursion1(int userSum) 
    {
        
        if (userSum == 0) {
            return userSum;
            } else {
            return userSum + Recursion1(userSum - 1);
            }

    }

When you call this method with an int x, it will give you the sum of all the numbers from x to 0.

So if you call it with input 3, it will give you 3 + 2 + 1 + 0 = 6

What you want, is to use a recursive function to sum through all the numbers inputed by the user.

One way to accomplish that is as follows:

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class Main {
    
    public static int sumByRecursion(List<Integer> inputList) {
        if (inputList.size() == 1) {
            return inputList.get(0);
        } 
        else {
            return inputList.get(0) + sumByRecursion(inputList.subList(1,inputList.size()));
        }
    }       
    
    public static void main(String[] args) 
    {
        List<Integer> inputList = new ArrayList<>();
        Scanner scan = new Scanner(System.in);
        System.out.println("Program Started");
        for(int i=0; i < 5; i++) {
            //ask user input 
             System.out.print("Enter any number: ");
             inputList.add(scan.nextInt());
        }
        scan.close();
        int sumNum = sumByRecursion(inputList);
        System.out.println("The sum of digits is: "+sumNum);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

-1

Is this what you want?

 for(i=0;i < 5;i++) {
     //ask user input 
     System.out.print("Enter any number: ");
     userSum += scan.nextInt();
 }

 System.out.println("The sum of digits is: " + userSum);

1 Comment

normally yes, but they are asking for a recursive method. This is most likely a homework question.

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.