-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVarargsVariableArguments.java
More file actions
42 lines (37 loc) · 1.12 KB
/
VarargsVariableArguments.java
File metadata and controls
42 lines (37 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class VarargsVariableArguments {
public static void main(String[]args){
/*varargs=> variable arguments
allow method to accept a varying number of arguments
they make methods more flexible, no need for overloaded methods
method overload is having a method with same names but different parameters
java packs the arguments into an array
add > ...(ellipsis)
*/
// System.out.println(add(1,2,3,3));
System.out.println(average(1,2,2,23));
System.out.println(average());//passing no arguments
}
// static int add(int... numbers){
//
//
// int sum=0;
//
// for(int number:numbers){
// sum+=number;
// }
//
// return sum;
//
// }
static double average(double... numbers){
double sum =0;
if(numbers.length==0){
System.out.print("no argument passed hence answer is");
return 0;
}
for(double number: numbers){
sum+=number;
}
return sum / numbers.length;
}
}