Link Search Menu Expand Document

Exercise: By Two

Create an int named myNumber and assign the value 17 to it. Write code that prints the following, where the last number in each line is the result of a computation.

17 plus 2 is 19
17 minus 2 is 15
17 multiplied by 2 is 34
17 divided by 2 is 8
17 modulo 2 is 1

Now change so that you use the number 18 instead of 17 - you should only have to change in one place!

  • The new output should look like below
    18 plus 2 is 20
    18 minus 2 is 16
    18 multiplied by 2 is 36
    18 divided by 2 is 9
    18 modulo 2 is 0
    

Exercise: By Three

  • Change the code you wrote in “By Two” so that you use 3 instead of two.
  • Do you have to change in more than one place? How could you change your code so that you only need to change in one place?
  • After you have updated the code so that you only need to change it in one place test it out by changing from 3 to 4.

You should see the following output

18 plus 4 is 22
18 minus 4 is 14
18 multiplied by 4 is 72
18 divided by 4 is 4
18 modulo 4 is 2