Skip to content

Commit 57112e5

Browse files
authored
Create JavaVarags.java
1 parent 6bd53ed commit 57112e5

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Advanced/JavaVarags.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Author: Atharv Damle
2+
// Create a function to accept variable arguments and calculate and print their sum.
3+
// Full Question: https://www.hackerrank.com/challenges/simple-addition-varargs/problem
4+
5+
class Add {
6+
// ...a signifies a variable argument function. It accepts a variable number of integers
7+
// and converts them to an int array.
8+
void add(int ...a)
9+
{
10+
int sum = 0;
11+
for (int i = 0; i < a.length - 1; i++)
12+
{
13+
sum += a[i];
14+
System.out.print(a[i] + "+");
15+
}
16+
sum += a[a.length - 1];
17+
System.out.println(a[a.length - 1] + "=" + sum);
18+
}
19+
}

0 commit comments

Comments
 (0)