Java Varargs - Simple Addition

  • + 0 comments
    //Write your code here
    class Add{
        public void add(int ...a){
            int sum=0;
            for(int i=0;i<a.length;i++){
                if(i<a.length-1){
                    System.out.print(a[i]+"+");
                }
                sum=sum+a[i];
            }
            System.out.print(a[a.length-1]+"="+sum);
            System.out.println();
        }   
    }