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.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

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

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Junior Member dak2007's Avatar
    Join Date
    Apr 2010
    Thanked 0 Times in 0 Posts

    Default Re: Odd and Even Numbers Logics in FOR and IF Loop methods


    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.


  3. #3
    Forum old-timer
    Join Date
    Nov 2008
    Faversham, Kent, UK
    My Mood
    Thanked 58 Times in 54 Posts

    Default 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;

Similar Threads

  1. How To: Add line numbers to your JTextArea
    By Freaky Chris in forum Java Swing Tutorials
    Replies: 11
    Last Post: October 15th, 2013, 10:22 PM
  2. help me .. about creating random numbers
    By soldier in forum Java Theory & Questions
    Replies: 2
    Last Post: December 20th, 2009, 08:24 PM
  3. Roman Numbers
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: December 12th, 2009, 02:19 AM
  4. Working with Methods
    By duckman in forum Object Oriented Programming
    Replies: 3
    Last Post: November 9th, 2009, 08:27 PM
  5. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 04:36 AM

Tags for this Thread