Variable length argument
Write a program to input number of argument function.
import java.util.Scanner;
class P18VariableLengthArray
{
public static void main()
{
System.out.println("\nSUM of Numbers "+ sum(1,2));
System.out.println("\nSUM of Numbers "+ sum(1,2,3));
System.out.println("\nSUM of Numbers "+ sum(1,2,3,4));
System.out.println("\nSUM of Numbers "+ sum(1,2,6,8,9,5));
}
static int sum(int... no)
{
int sum=0;
for(int i=0; i<no.length; i++)
sum +=no[i];
return sum;
}
}
Comments
Post a Comment