Given an array of numbers (a list in groovy), determine whether the sum of all of the numbers is odd or even.
Give your answer in string format as 'odd' or 'even'.
If the input array is empty consider it as: [0] (array with a zero).
public class Codewars {
public static String oddOrEven (int[] array) {
int sumNum=0;
String num;
for(int i =0;i<array.length;i++){
sumNum=sumNum+array[i];
}
if(sumNum %2 ==0){
num="even";
}else{
num="odd";
}
return num;
}
}
https://www.codewars.com/kata/5949481f86420f59480000e7/train/java
网友评论