0% found this document useful (0 votes)
11 views1 page

Variable Length Program

This Java program demonstrates the use of variable length arguments using the varargs method, allowing a method to accept a variable number of arguments of the same type. The sumNumber method calculates the sum of all integer arguments passed to it using a varargs parameter and for-each loop, and main calls this method with different numbers of arguments to test it.

Uploaded by

study.drizzle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views1 page

Variable Length Program

This Java program demonstrates the use of variable length arguments using the varargs method, allowing a method to accept a variable number of arguments of the same type. The sumNumber method calculates the sum of all integer arguments passed to it using a varargs parameter and for-each loop, and main calls this method with different numbers of arguments to test it.

Uploaded by

study.drizzle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

//variable length program using vargars method

class VarargExample {
public int sumNumber(int ... args){
System.out.println("argument length: " + args.length);
int sum = 0;
for(int x: args){
sum += x;
}
return sum;
}
public static void main( String[] args ) {
VarargExample ex = new VarargExample();
int sum2 = ex.sumNumber(2, 4);
System.out.println("sum2 = " + sum2);
int sum3 = ex.sumNumber(1, 3, 5);
System.out.println("sum3 = " + sum3);
int sum4 = ex.sumNumber(1, 3, 5, 7);
System.out.println("sum4 = " + sum4);
}
}

You might also like