Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

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

1. ## 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

2. ## 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.

3. ## Re: Odd and Even Numbers Logics in FOR and IF Loop methods

You are right Dak.

I think the loop can be improved:
```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;
}
}
}```