Find the last even in java


In some java interview, most asked question is find the last even of the given array, now we are going to solve this problem.

package one;

public class LastEven {

public static int[] arr = { 28, 3, 4, 6, 10, 11 ,11,23,22,3};

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Last evend=" + lastEven(arr));
}

public static int lastEven(int[] arr) {
int lE = -1;
for (int x = 0; x < arr.length; x++) {
if (arr[x] % 2 == 0) {
lE = arr[x];
}
}
return lE;
}

}


Output:

Last evend=22

1 comment:

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete