Hey all
This comment very useful for beginners
I have shown the Odd and Even Numbers Logics Programs in FOR and IF Loop Condition, please Click here
Printable View
Hey all
This comment very useful for beginners
I have shown the Odd and Even Numbers Logics Programs in FOR and IF Loop Condition, please Click here
Hi,
I saw your program. While we loop the numbers why can't you have a single IF and ELse condition so that in If's part it would be even number . If the quotient is not zero its going to go into else part and it would increase the counter for Odd.
I am too a begineer in Java programming. If i am wrong please suggest me.
Thanks,
DAK.
You are right Dak.
I think the loop can be improved:Code :int o = 0; int e = 0; for (int i = 1; i <= n; i++) { if (i % 2 == 0) { even[e++] = i; if (e == 2) { sb.append(even[0]).append(" ").append(even[1]).append(", "); e = 0; } } else { odd[o++] = i; if (o == 2) { sb.append(odd[0]).append(" ").append(odd[1]).append(", "); o = 0; } } }