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.
public void add(int... n){
int sum = 0;
StringBuffer sb = new StringBuffer();
for(int i : n){
sum += i;
sb.append(i).append("+");
}
sb.deleteCharAt(sb.length()-1);
System.out.println(sb.append("=").append(sum));
}
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 →
public void add(int... n){ int sum = 0; StringBuffer sb = new StringBuffer(); for(int i : n){ sum += i; sb.append(i).append("+");
} sb.deleteCharAt(sb.length()-1); System.out.println(sb.append("=").append(sum)); }