CS 111 - Introduction to Computer Science
Homework #9


Due: Friday 10/30 at the beginning of class

1. Divisors:  In a class Divisors.java, read in a maximum integer n and use nested loops to print a list of divisors for each of 1 through n as follows (input is bold and underlined):

Largest integer? 100
1: 1 
2: 1 2 
3: 1 3 
4: 1 2 4 
 
...
 
95: 1 5 19 95 
96: 1 2 3 4 6 8 12 16 24 32 48 96 
97: 1 97 
98: 1 2 7 14 49 98 
99: 1 3 9 11 33 99 
100: 1 2 4 5 10 20 25 50 100 

2. Greatest Common Divisor:  In a class GCD.java, read in two integers a and b.  Compute and print their greatest common divisor (GCD) using the Euclidean algorithm.   The Euclidean algorithm is simply described as follow:  While b is nonzero,  set a' to b and set b' to (a modulus b), where a' and b' are the new values of a and b for the next iteration.  Hint: One can accomplish this using only three variables: a, b, and one temporary variable for storage.  Your output should have the following format:

Integer a? 504
Integer b? 2226
Greatest common divisor of a and b: 42

3. Text Diamond: Exercise 5.24.  Call your program Diamond.java.  If you need an optional warm-up exercise, consider 5.15.   Hint: One possible solution uses two nested for repetitions, a single if decision, and three print statements (one each for '*', ' ', and newline).

Extra credit (10%): Generalize your program to prompt the user for an odd positive integer n and produce an n-by-n diamond shape.