Monday, December 31, 2018

Java program using var args

package example;

public class varargs {

public static void main(String[] args) {
System.out.println("1:" + sum(1, 2, 3));
System.out.println("2:" + sum(1, 2, 3, 6, 7, 0));

}

public static int sum(int... a) {
int sum1 = 0;

for (int i : a) {
sum1 = sum1 + i;
}
return sum1;

}

}


Output: 

1:6
2:19

No comments:

Post a Comment