Homework 6

Out: Tue 26 Feb 1998
Due: During Lab Session, or in 1 week

Programming Assignment:

Design and debug the Divisible.java program which prompts the user for three integers: a, b and upTo and prints out the number of integers from 1 to upTo which are:

  1. Divisible by both a and b,
  2. Divisible by either a or b.
Check the input to make sure that all integers are 1 or greater. Warn the user and exit if not.

Note: Use while loops - for loops will be talked about in a future class.

The modulus operator % is the best way to check for divisibility. An integer i is divisible by an integer j if:

      if ( ( i%j ) == 0 ) {
           // j divides i
      }

That is, the remainder when i is divided by j is zero. If either i or j are negative, the result has the sign of the i.
   5%3 produces 2
   5%(-3) produces 2
   (-5)%3 produces -2
   (-5)%(-3) produces -2
It is done this way so that
   ((i/j)*j + (i%j)) == i
is always true. (Integer division rounds towards zero.)

Use PostIt to submit your homework. There are further instructions about how to do this.