-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodSum.java
More file actions
38 lines (28 loc) · 865 Bytes
/
MethodSum.java
File metadata and controls
38 lines (28 loc) · 865 Bytes
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
//4.Write a function /method which takes variable no of int numbers as an argument
//and returns the sum of these arguments as an output.
import java.util.Scanner;
import java.util.Arrays;
public class MethodSum {
private static int sumArray(int... arr) {
int sum=0;
for(int index=0;index<arr.length;index++) {
sum+= arr[index];
}
return sum;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int size,sum ;
System.out.print("Enter Array Size :");
size=input.nextInt();
int arr[ ] = new int[ size ];
for(int index = 0; index<arr.length;index++) {
System.out.print("Enter Array Elements :");
arr[ index ] = input.nextInt();
}
System.out.println("Array is : "+Arrays.toString(arr));
sum=MethodSum.sumArray(arr);
System.out.println("Sum :"+sum);
input.close();
}
}