Problem:
Return the number of even ints in the given array. Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1.
countEvens({2, 1, 2, 3, 4}) → 3
countEvens({2, 2, 0}) → 3
countEvens({1, 3, 5}) → 0
Solution:
public int countEvens(int[] nums) { int even = 0; for (int count=0; count < nums.length; count++) { if (nums[count] % 2 == 0) even++; } return even; }
public int countEvens(int[] nums) {
ReplyDeleteint even = 0;
for(int count=0; count < nums.length; count++)
{
if(nums[count] % 2 == 0)
{
even++;
}
}
return even;
}
please provide tracing also
ReplyDeletepublic int countEvens(int[] nums) {
ReplyDeleteint count=0;
for(int i=0;i<nums.length;i++){
if(nums[i]%2==0){
count++;
}
}
return count;
}
public final int countEvens(int[] nums) {
ReplyDeleteint evenCount = 0;
for(final int i: nums){
if(i % 2 == 0){
evenCount ++;
}
}
return evenCount;
}
public final int countEvens(int[] nums) {
ReplyDeleteint evenCount = 0;
for(final int i: nums){
if(i % 2 == 0){
evenCount ++;
}
}
return evenCount;
}
public final int countEvens(int[] nums) {
ReplyDeleteint evenCount = 0;
for(final int i: nums){
if(i % 2 == 0){
evenCount ++;
}
}
return evenCount;
}
public int countEvens(int[] nums) {
ReplyDeleteint count = 0;
for(int i : nums){
if(i % 2 == 0)
count++;
}
return count;
}
public int countEvens(int[] nums) {
ReplyDeleteint EvenCounter=0;
if(nums.length!=0||nums!=null){
for(int i=0;i<nums.length;i++){
if(nums[i]%2==0){
EvenCounter+=1;
}
}
}
return EvenCounter;
}