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.
//Write your code here
class Add{
public static void add(int ...n){
int result=0;
for(Integer i:n){
result+=i;
System.out.print(i);
if(i==n[n.length-1]) {
System.out.print("=");
System.out.println(result);
break;}
System.out.print("+");
}
}
}
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 →
import java.io.; import java.lang.reflect.; import java.util.; import java.text.; import java.math.; import java.util.regex.;
//Write your code here class Add{ public static void add(int ...n){ int result=0; for(Integer i:n){ result+=i; System.out.print(i); if(i==n[n.length-1]) { System.out.print("="); System.out.println(result); break;} System.out.print("+");
}