Variable Arguments (VarArgs) in Java
package com.company;
//import java.util.Scanner;
public class tuts {
static int sum(int...arr){
int result = 0;
for (int a: arr){
result += a;
}
return result;
}
public static void main(String[] args) {
System.out.println("The Sum of 1+2+3+4+5+6+7+8+9+10 is: "+ sum(1,2,3,4,5,6,7,8,9,10) );
System.out.println("The sum of nothing is: " + sum());
}
}
Comments
Post a Comment