Assignment 3

This assignment will give you some practice into writing simple programs that defines functions and uses conditional statements (i.e. if-elif-else).

In this assignment, you will write two programs. Your programs should be runnable at the DOS command prompt by calling python program name.

 

Program 1. Greatest Common Divisor

The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the two numbers. Write a function called gcd that accepts two integers and returns the greatest common divisor of them.

Your main program should ask the user for two integers between 1 and 100. It should then output the greatest common divisor returned by the function. Your program should ensure that the two integers are within the valid range (i.e. between 1 and 100 inclusive). If not, keep asking the user to re-enter the integer until the integer is valid.

A sample run of your program should look like the following:

J:\> python gcd.py
This program finds the greatest common divisor.
Enter the first number: 18
Enter the second number: 84
The greatest common divisor of 18 and 84 is 6.
 
J:\> python gcd.py
This program finds the greatest common divisor.
Enter the first number: 101
Invalid number! Enter the first number again: 18
Enter the second number: 0
Invalid number! Enter the second number again: 84
The greatest common divisor of 18 and 84 is 6.
 

Your program should be called gcd.py.

 

Program 2. Leap Year

A year will be a leap year if it is divisible by 4 but not by 100. If a year is divisible by 4 and by 100, it is not a leap year unless it is also divisible by 400. Thus years such as 1996, 1992, 1988 and so on are leap years because they are divisible by 4 but not by 100. For century years, the 400 rule is important. Thus, century years 1900, 1800 and 1700 while all still divisible by 4 are also exactly divisible by 100. As they are not further divisible by 400, they are not leap years.

Write a function that takes an integer value representing a year and print out whether or not the year is a leap year. The function should also state the reason why the year is not a leap year (either it is not divisible by 4 or it is divisible by 4 and 100, but not by 400).

Your main program should ask the user for an integer representing a year and then call the function to print out the result. A sample run of your program should look like the following:

J:\> python leapyear.py 
This program determines whether or not the input year is a leap year.
Enter the year: 1996
Year 1996 is a leap year.
 
J:\> python leapyear.py 
This program determines whether or not the input year is a leap year.
Enter the year: 2001
Year 2001 is not a leap year because it is not divisible by 4.
 
J:\> python leapyear.py 
This program determines whether or not the input year is a leap year.
Enter the year: 1900
Year 1900 is not a leap year because it is divisible by 4 and 100 but not by 400.
 

Your program should be called leapyear.py.

Due Date

Your homework is due before 7am, Monday November 23, on WebCT (Hand in two separate programs). No exceptions. We will not accept late handins any more.

Anti-Cheating Policy

This is to remind you that COMP 201 has a strict no-cheating policy. The penalties for cheating are on the course webpage. Please reread those policies -- don't say that we didn't warn you!