We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
class Add{
public void add(int... a){
int sum = 0;
int plus = a.length-1;
for(int x:a){
sum += x;
System.out.print(x);
if(plus>0){
System.out.print("+");
plus--;
}
}
System.out.print("="+sum);
System.out.println();
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Varargs - Simple Addition
You are viewing a single comment's thread. Return to all comments →
class Add{
public void add(int... a){ int sum = 0; int plus = a.length-1; for(int x:a){ sum += x; System.out.print(x);
if(plus>0){ System.out.print("+"); plus--; }
} System.out.print("="+sum); System.out.println(); } }