In this HackerRank Java Varargs - Simple Addition problem your task is to create the class Add and the required methods so that the code prints the sum of the numbers passed to the function add.

HackerRank Java Varargs - Simple Addition problem solution

 HackerRank Java Varargs - Simple Addition problem solution

class Add {
    void add(int... vars) {
        int sum = 0;
        String result = "";
        for (int i = 0; i < vars.length; i++) {
            sum +=vars[i];
            if (i != vars.length - 1)
            result += vars[i] + "+";
            else
               result += vars[i];
        }
        result += "=" + sum;
        System.out.println(result);
    }
}