Java Varargs - Simple Addition

  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Integer> arr = new ArrayList<>();
        while(sc.hasNextInt()){
            arr.add(sc.nextInt());
        }
    
        int sum = arr.get(0);
        String ques = ""+arr.get(0);
    
        for(int i=1; i<arr.size(); i++){
            int val = arr.get(i);
            ques+="+" + val;
            sum+=val;
            if(i!=3)
                System.out.println(ques+"="+sum);
        }
    }
    

    }