# Odd and Even Numbers Logics in FOR and IF Loop methods

• January 18th, 2010, 04:21 AM
mparthiban
Odd and Even Numbers Logics in FOR and IF Loop methods
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
• April 27th, 2010, 06:56 AM
dak2007
Re: Odd and Even Numbers Logics in FOR and IF Loop methods
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.
• May 12th, 2010, 05:19 AM
dlorde
Re: Odd and Even Numbers Logics in FOR and IF Loop methods
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; } } }```