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

2 comments:

  1. I used to purchase cheap paper service works about java, 'cause I'm not good at it, but with yours site I think I can do my works myself! Thank you very much! Your tutorials are pretty simple, but full and very useful. I love how you explain hard parts. Even I can get it, and it's a deal, coz I'm really bad with programming.

    ReplyDelete