-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumArrays.java
More file actions
25 lines (24 loc) · 867 Bytes
/
SumArrays.java
File metadata and controls
25 lines (24 loc) · 867 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
//8th kyu
//Write a method that takes an array of numbers and returns the sum of the numbers.
//The numbers can be negative or non-integer.
//If the array does not contain any numbers then you should return 0.
//Assumptions: You can assume that you are only given numbers.
//You cannot assume the size of the array.
//You can assume that you do get an array and if the array is empty, return 0.
//Example: [1, 5.2, 4, 0, -1] => 9.2
public class SumArrays {
public static double sum(double[] numbers) {
double count = 0.0;
if (numbers.length == 0){
return 0.0;
}
for (int i = 0; i < numbers.length; i++){
count += numbers[i];
}
return count;
}
public static void main(String[] args){
double[] w = {1.0, 2.0, 3.0, 4.0, 5.0};
System.out.println(sum(w));
}
}