• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with while loop involving odd and even integers  RSS feed

 
Pointdexter Huynh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, guys! I'm new to the forums and to java. I need help with what to do next on my code.
My instructions are to:
1. Prompt the user to input two positive integers: firstNum and secondNum (firstNum must be
smaller than secondNum).
2. Output all the even numbers between firstNum and secondNum inclusive.
3. Output the sum of all the even numbers between firstNum and secondNum inclusive.
4. Output all the odd numbers between firstNum and secondNum inclusive.
5. Output the sum of all the odd numbers between firstNum and secondNum inclusive.
*Use while loop



I honestly don't understand what to do with the while loop and how to find even and odd numbers.
 
Carl McGraw
Greenhorn
Posts: 7
1
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First things first, welcome to the ranch.

I will try to answer your question to the best of my abilities. But I don't think you're asking quite what you think you want.

Pointdexter Huynh wrote:I honestly don't understand what to do with the while loop and how to find even and odd numbers.


I am confused. So you are effectively using the while loop here:



Then inside you're checking, using modulo:



That statement in the if is the modulo. You are checking if a given value's remainder when divided by two, is 0 (since modulo or % gives you the remainder when divided by the number on the right).



So using mod you can check the remainder of a number. All even numbers by definition are divisible by 2, and all non even (i.e, odd) are not divisible by two.

However there are a couple problems with the code you posted. First you said you wanted:

Pointdexter Huynh wrote:
2. Output all the even numbers between firstNum and secondNum...


So your loop should go from firstNum to secondNum. But in your loop you're comparing to secondNum only.

you should instead consider comparing to firstNum (Because that is what the problem is asking exactly.



But we also see a problem occurring here. Lets look at some values "going into" the while loop, and consider what they will do:



The first thing you're missing is "changing firstNum".

You need something along these lines as the first step to solving this problem:



There are other problems. But once you get past this first one at least your program will run. (Well.. you may have to define sumE and sumO as 0 first)
 
Pointdexter Huynh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so I changed secondNum to firstNum and have entered an infinite loop. How do I define the even/odd integers between the two keyboard numbers I enter? Should make another while loop or add more if/else statements inside my loop?
My code now:


And if this help this is an example of how it should look like once I compile it:
Enter an integer:
3
Enter another integer larger than the first one:
10
Even numbers: 4 6 8 10
Sum of even numbers = 28
Odd numbers: 3 5 7 9
Sum of odd numbers = 24
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see anything in the instructions that says you can only use one while loop.

One key to better programs is to keep things simple. Do one thing at a time. The more you try to do at once, the more complicated things get. The more complicated things get, the harder it is for you to wrap your head around what you're really telling the computer to do. Bugs are simply a misunderstanding between you and the computer. They're a failure to communicate properly. You think you're telling it to do something but the instructions you wrote (the program) are telling it to do something else. So, while you're still learning how to communicate properly with the computer, keep your instructions simple so you know you're telling the computer to do exactly what you think you want it to do.

Next, a while loop will only terminate when the loop condition is false. If there's nothing inside the loop makes the loop condition false, then it will continue to repeat the instructions inside the loop. At some point, something needs to happen so that the condition of the while loop becomes false so it can stop repeating.



 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pointdexter Huynh wrote:Ok, so I changed secondNum to firstNum and have entered an infinite loop.

That's not what he meant.

Your loop condition is (firstNum <= secondNum)

The value of one or both of these have to change at some point so that this expression is no longer true and the loop can terminate.

However, because the names are what they are, they're going to mess up your thought process in coming up with the correct logic. I suggest you use another variable to help control the loop, a variable whose name suggests that it will change throughout the loop execution. Try using currentNum instead, as in "the number to use in the current loop iteration". You want to say "While the number we're currently on is less than or equal to the second number the user entered, do these things: ..." And before entering the loop, you tell the computer "We're currently on the first number the user entered"

You could also use a name like x and think about it this way: Let x be a number that changes every time we execute the loop instructions. Starting with x equal to the first number the user entered, do blah blah blah. Then make x be the next number. Repeat this process if the new value of x is less than or equal to the second number the user entered, otherwise you're done.

Whether to use x or currentNum or some other name is a matter of preference. Note however, that variable names like "x" are not as expressive (they usually don't convey a clear idea) and should be used with care. See if you think this is more expressive:

Let currentNum be the number that we're currently using to execute the loop instructions. Starting with currentNum as the first number the user entered, do blah blah blah. Then make currentNum be the next number. Repeat this process if the new value of currentNum is less than or equal to the second number the user entered, otherwise you're done.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And just because you'd like your output to look like this:

Even numbers: 4 6 8 10
Sum of even numbers = 28
Odd numbers: 3 5 7 9
Sum of odd numbers = 24

it doesn't mean your program has to do this:

The code that produces the desired output could very well end up looking something like this:

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!