Saturday, February 15, 2014

SOLUTION Exercise 2: conditionals

Full exercise post

Ask the user for a number. Depending on whether the number is even or odd, print out an appropriate message to the user. Hint: how does an even / odd number react differently when divided by 2?

Extras:

  • If the number is a multiple of 4, print out a different message.
  • Ask the user for two numbers: one number to check (call it num) and one number to divide by (check). If check divides evenly into num, tell that to the user. If not, print a different appropriate message.

  • Sample solution

    There are many ways of doing the exercise, so I am posting a few sample solutions. The very basics:

    And something that looks slightly more complex (but is just a more complicated conditional):


    No comments:

    Post a Comment