Week 1: Exercises

1. Not Divisible by 3, 5, 7

Write a program that prints the sum of all integers in the range 0 ≤ i ≤ 1,000,000 such that i is not divisible by any of the numbers {3, 5, 7}.

2. Smallest Multiple

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? Either write a program to determine the answer, or compute it mathematically.

3. Palindrome Test

Write a program that reads a string from the console and prints "palindrome" if it is a palindrome, otherwise "not"'.

4. Capitalized Words

Write a program that reads a string and writes it back out, capitalizing the first letter of every word.

5. Hailstone Sequence

A hailstone sequence of integers Hi starting from any integer H1 is defined as follows: If Hi is even, then Hi + 1 = Hi / 2; otherwise Hi + 1 = 3 Hi + 1. For example, the sequence starting from H1 = 10 is 10, 5, 16, 8, 4, 2, 1, 4, 2, 1, ...

Write a program that determines and prints the first value k for which the hailstone sequence starting at k rises above 1,000,000,000.

6. S Begins With T

Write a program that reads two strings S and T, each on its own line. The program should print "true" if S starts with T, "false" otherwise.

7. Exponentiation

Write a program that reads an integer A and an integer B ≥ 0 and prints the value of AB. Implement exponentiation using repeated multiplication.

8. Decimal to Binary

Write a program that reads a decimal integer and prints it out in base 2, i.e. binary.

9. Making Change

Read a price in Czech crowns. Print out a combination of 20-Kč, 10-Kč, 5-Kč and 1-Kč coins that add up to the price, using the smallest possible number of coins.

Enter price: 67
20 Kc: 3
10 Kc: 0
5 Kc: 1
1 Kc: 2