Java Varargs - Simple Addition

  • + 0 comments

    class Add{

        public void add(int... arr){
        int sum = arr[0];
        System.out.print(arr[0]);
        for(int i=1;i<arr.length-1;i++){
    
        System.out.print("+"+(arr[i]));
          sum=sum+arr[i];
        }
        sum=sum+arr[arr.length-1];
        System.out.print("+"+arr[arr.length-1]+"="+sum);
        System.out.println();
    }
    

    }