Home > Software engineering >  Not able to calculate sum in recursive java function
Not able to calculate sum in recursive java function

Time:12-18

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.");
    }

}

CodePudding user response:

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);
    }
}

CodePudding user response:

This should work as a recursive function. If you require any clarification let me know.

class MyClass {

    private final Scanner scan = new Scanner(System.in);

    public int sum(int count){
        System.out.print("Enter any number: ");
        int userSum = scan.nextInt();
        if(count == 4) {
            scan.close();
            return userSum;
        }
        count  ;

        return userSum   sum(count);
    }

    public static void main(String[] args)
    {
       MyClass r = new MyClass();

       int sumNum =  r.sum(0);

        System.out.println("The sum of digits is: " sumNum);
        System.out.println("Scanner Closed.");
    }

}

CodePudding user response:

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);
  • Related