0

So I have this problem.

Input # of rooms: 4
room1:6
room2:4
room3:7
room4:3

(if I type 5 in "Input # of rooms" there would also be room5)

Odd: 7 3
Even: 6 4

I have to display the odd and even numbers, so I came up with this code:

System.out.print("Input # of rooms: ");
int rms=Integer.parseInt(io.readLine());

int[] array=new int[rms];
int a=0;
int b=1;


do {

    System.out.print("room "+b+":");
    array[a] = Integer.parseInt(io.readLine());
    a++;
    b++;

} while (a<rms);

I don't know how to display which are Odd numbers and which are Even numbers?

1
  • I take it you're using Java? Commented Jul 19, 2013 at 2:21

2 Answers 2

5

you want to find the remainder or modulus when the param is divided by 2.

3 % 2 = 1 so odd 4 % 2 = 2 so even

if(param % 2 == 1){
  Print odd number
}else{
  Print even number
}

Should get you started

Sign up to request clarification or add additional context in comments.

Comments

1

The use of the modulo operator (%) will be invaluable here - it performs integer division and returns the remainder of the quotient - kind of like short division.

The rules for determining the type of number are simple:

  • If the number is even, it is divisible by 2.
  • Otherwise, it is odd.

As for the printing part: I would recommend accumulating the values in two separate StringBuffers or Strings if you prefer, adding a space between when we get another of the type of value we want. Then, we can print it out pretty after we're done iterating through the array.

One last thing - you should only need one loop - preferably a for loop, since you know exactly how many elements you're going to iterate over. You can use the above rules for modulus to determine which number gets appended to which variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.