loops :: problem set 3

Make sure to check for the validity of the data inputted by the user

  1. In the Gregorian calendar, the current standard calendar in most of the world, most years that are evenly divisible by 4 are leap years. In each leap year, the month of February has 29 days instead of 28. Adding an extra day to the calendar every four years compensates for the fact that a period of 365 days is shorter than a solar year by almost 6 hours.However, some exceptions to this rule are required since the duration of a solar year is slightly less than 365.25 days. Years that are evenly divisible by 100 are not leap years, unless they are also evenly divisible by 400, in which case they are leap years. Have the user enter a four digit year and then output whether the year is (or isnít) a leap year. Your program should prompt the user for inputs until -1 is entered. Moreover, your program should only allow inputs between the years 1800 and 2180.


    Enter year:
    Leap Year
    Enter year:
    Incorrect value
    Enter year:
    No Leap Year
    Enter year:
  2. Here (see illustration below) is a game board for the game Snakes and Ladders. Each player throws a pair of dice to determine how many squares his/her game piece will advance. If the piece lands on the bottom of a ladder, the piece moves up to the square at the top of the ladder. If the piece lands on the top of a snake, the piece "slides" down to the square at the bottom of the snake. If the piece lands on the last square, the player wins. If the piece cannot advance the number of squares indicated by the dice, the piece is not moved at all. In order to help you play this game via a cell phone while travelling, you will write a program that simulates your moves on the board shown and, of course, runs on your handheld computer. You will repeatedly throw the dice and enter the result into the program. After each throw the program will report the number of the square where your piece lands. When the program starts it should assume the piece is on square 1. It should repeatedly read input from the user (a number between 2 and 12) and report the number of the square where the piece lands. In addition, if the piece moves to the last square, the program should print "You Win!" and terminate. If the user enters 0 instead of a number between 2 and 12, the program should print "You Quit!" and terminate. For clarity, you are to use the board pictured below and you should note that the board has 3 snakes (from 54 to 19, from 90 to 48 and from 99 to 77) and 3 ladders (from 9 to 34, from 40 to 64 and from 67 to 86).

      snakes and ladders

  3. In Dolphin City, a mathematical place to live, the mayor is elected every 4 years, the treasurer is appointed every 2 years, the chief programmer is elected every 3 years and the dogcatcher is replaced every 5 years. This year, Year X, the newly elected mayor announced the appointment of the new treasurer, a new dog-catcher and congratulated the chief programmer for winning the recent election. That is, all positions were changed over. This is highly unusual. You will quantify how unusual this really is. Write a program that inputs the year X and the future year Y and lists all years between X and Y inclusive when all positions change.

    Sample Input

    Enter the current year:
    Enter a future year:

    Sample Output

    All positions change in year 2004
    All positions change in year 2064
For answers click here