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


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

Results 1 to 3 of 3

Thread: Odd Even - For loop

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Odd Even - For loop

    I need help with this code, my desired output is supposed to be like this

    Input an integer:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    0
    Even numbers are: 0 2 4 6 8
    Odd numbers are: 1 3 5 7 9

    But my output with this code is like this:

    Input an integer: 1
    Input an integer: 2
    Input an integer: 3
    //and so on
    Even numbers are 0
    Even numbers are 2
    Odd numbers are 1
    Odd numbers are 3
    //and so on

    Here's my code

    import java.io.*;
    public class Prelims2{
    public static void main(String[] args)throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int a, b;
    int al[] = new int[10];
     
    for(b = 0; b < 10; b++){
    System.out.print("Input an integer: ");
    al[b]=Integer.parseInt(br.readLine());
    }
    for(b = 0; b < 10; b++)
    if(al[b]%2==0){
    System.out.println("Odd numbers are:" +b);
    }
    {
    for(b = 0; b < 10; b++)
    if(al[b]%2!=0)
    System.out.println("Even numbers are:" +b);
    }
    }
    }
    Last edited by clevel211; October 11th, 2010 at 01:17 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Odd Even - For loop

    Move the print out requesting the user to enter an integer outside of the for loop and it will only print once instead of each time around the loop.
    You'll need a way to save the even and odd numbers that you detect in the loop.
    One way is to have separate arrays to hold the even and the odd numbers as you go around the loop. Then at the end of the loop you can have more loops to print the contents of one array and then the contents of the other array.
    Another way is to concatenate the number onto separate Strings (even and odd) to be displayed after the data gathering loop.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Odd Even - For loop

    Thanks.. I already done it..

    Thanks for the help

Similar Threads

  1. help with a for loop
    By newbie79 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 3rd, 2010, 02:20 AM
  2. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  3. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM
  4. loop or what
    By silverspoon34 in forum Loops & Control Statements
    Replies: 5
    Last Post: November 19th, 2009, 02:10 PM
  5. can any one do this in a loop? (for loop)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: October 4th, 2009, 08:31 PM