-----------------------------------------------------------------
public class IfElse {
public static void main(String[]args){
int numb [] = {1,2,3,4,5,6,7,8,9,0,111,222,444,888,999,666}; // our list
for(int x = 0; x <numb.length;x++)
if ((numb[x] %2 )==0){
System.out.println(numb[x]+" is even number in this list.");
}
else{
System.out.println(numb[x]+" is odd number in this list.");
}
}
}
_________________________________________________
Output:
1 is odd number in this list.
2 is even number in this list.
3 is odd number in this list.
4 is even number in this list.
5 is odd number in this list.
6 is even number in this list.
7 is odd number in this list.
8 is even number in this list.
9 is odd number in this list.
0 is even number in this list.
111 is odd number in this list.
222 is even number in this list.
444 is even number in this list.
888 is even number in this list.
999 is odd number in this list.
666 is even number in this list.
________________________________________________
Above program is easy to understand but might be some of you confuse about "if ((numb[x] %2 )==0)", we write this because each time all number should be divided by "2", now you better know that which number could be divided to "2", of course even numbers only and rest of the number will be odd number and why we write "==0" this mean if we divided "2" with even number than remainder will be only "0" left and it prove that it is even number.
Now I hope your doubt is solved now,
Thank you for your time.........................
hi
ReplyDeleteHi Smitha !
Delete