Java Varargs - Simple Addition

  • + 0 comments

    class Add{

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

    }