Program:-
public class VarargsV33 {
/*static int sum(int a, int b){
return a + b;
}
static int sum(int a, int b, int c){
return a+b+c;
}
static int sum(int a, int b, int c, int d){
return a;
}*/
//in this way we can find the sum n numbers of numbers but making method for every number of sum required more time.
//so we have another method to do this is by variable argument(varargs) method as follows.
static int sum(int ...arr){
int result = 0;
for(int a:arr){
result +=a;
}
return result;
}
/*in the above method if we do not assign any value to the arr then it does not complain about it and just print the 0
value but if we do as follows then at least one value is required to be assign to the method
so the line System.out.println("sum of nothing is: " + sum()); becomes like this
System.out.println("sum of nothing is: " + sum(2));*/
/*static int sum(int a, int ...arr){
int result = a;
for(int b :arr){
result += b;
}
return result;
}*/
public static void main(String[] args) {
System.out.println("sum of nothing is: " + sum());
System.out.println("sum of two numbers 4 and 5 is: " + sum(4,5));
System.out.println("sum of three numbers 3,4 and 5 is: " + sum(3,4,5));
System.out.println("sum of four numbers 2,3,4 and 5 is: " + sum(2,3,4,5));
}
}

