Java Varargs - Simple Addition

  • + 0 comments

    class Add { void add(int... number) {

        int counter = 0;
        int sumNum = 0;
    
        for (int i = 0; i < number.length; i++) {
    
            sumNum = sumNum + number[i];
            counter++;
            if(counter<number.length){
                System.out.printf("%d+", number[i]);
            }else{
                System.out.printf("%d", number[i]);
            }
    
        }
        System.out.printf("=%d%n", sumNum);
    
    }
    

    }