public class Nodigeven {
static int even=0;
static int count=0;
static int findnodigeven(int[] arr) {
for(int i=0;i<arr.length;i++) {
while(arr[i]!=0) {
arr[i]= arr[i]/10;
count++;
}
if(count%2==0) {
even++;
}
}
return even;
}
public static void main(String[] args) {
int[] arr = {122,255,133,14,15,16};
System.out.println(findnodigeven(arr));
}
}
am getting wrong output i.e : 1 .. can you tell me where i am going wrong here in the code
>Solution :
You need to change where you initialize count in your code – it never resets back to 0 and therefore the logic is incorrect for detecting if a number has an even number of digits.
public class Nodigeven {
static int even = 0;
static int findnodigeven(int[] arr) {
for(int i = 0; i < arr.length; i++) {
int count = 0; // This variable was in the wrong spot!
while(arr[i] != 0) {
arr[i] = arr[i] / 10;
count++;
}
if(count % 2 == 0) {
even++;
}
}
return even;
}
public static void main(String[] args) {
int[] arr = {122,255,133,14,15,16};
System.out.println(findnodigeven(arr));
}
}