User Tools

Site Tools


cs175:loops_lab_-_oddeven

OddEven


  • Topics: Decisions, Loops

Write and save a program that reads an integer and determines if it's odd or even. An input of zero counts as neither odd nor even. Here are some sample user dialogs from running this program:

         Enter an integer:  77464
         That number is EVEN.

         Enter an integer:  773
         That number is ODD.
         
         Enter an integer:  0
         That number is neither odd nor even.

Use a loop in this program to repeatedly prompt for integers to check if they are even, odd or zero.

I recommend that you write everything but the loop first. So, first, get the program to input an integer and then output whether it is EVEN, ODD or neither, and test it to make sure it does this correctly. And then, wrap a while loop around the part of the program you want to be repeated.

Version 1: while(true) - OddEven1.java

  • This version uses a simple while(true) loop that repeats until the user manually terminates the program.

Version 2: while(true) with conditional break loop - OddEven2.java

  • This version uses a sentinel value-controlled while loop that allows the user to terminate the loop by entering -9999.
  • This version uses a while(true) loop with a conditional break statement to exit the while loop.

Version 3: while(sentinel check) loop - OddEven3.java

  • This version uses a sentinel value-controlled while loop that allows the user to terminate the loop by entering -9999.
  • The prompt for user input and user input has to be done both outside the while loop and inside the while loop.

cs175/loops_lab_-_oddeven.txt · Last modified: 2014/11/19 14:05 by jchung

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki