-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcwh_33_varargs.java
41 lines (34 loc) · 1.03 KB
/
cwh_33_varargs.java
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
package company;
public class cwh_33_varargs
{
// static int sum(int a, int b)
// {
// return a+b;
// }
// static int sum(int a, int b, int c)
// {
// return a+b+c;
// }
// static int sum(int a, int b, int c, int d)
// {
// return a+b+c+d;
// }
static int sum(int ...arr)
{
int result = 0;
for(int a : arr)
{
result += a;
}
return result;
}
// static void sum(int a, int ...arr) --> This can be used when one argument is compulsory and so on.
public static void main(String[] args)
{
System.out.println("\nThe sum of nothing is = " +sum());
System.out.println("\nThe sum of 2 and 3 is = " +sum(2, 3));
System.out.println("\nThe sum of 2, 3, and 4 is = " +sum(2, 3, 4));
System.out.println("\nThe sum of 2, 3, 4, and 5 is = " +sum(2, 3, 4, 5));
System.out.println("\nThe sum of 2, 3, 4, 5, and 6 is = " +sum(2, 3, 4, 5, 6)+ "\n");
}
}