Week 1: Exercises

This is a set of elementary programming tasks that you should be able to implement in Free Pascal. The easiest tasks are at the beginning and they get somewhat harder as they progress.

I've included sample output below each task. (In some cases the sample output contains several program runs, separated by ===.)

1. Paint

Read integers representing the width, length and height of a room in meters, and print out the number of square meters of paint required to paint the walls and ceiling (but not the floor).

Width: 5
Length: 8
Height: 4
You need 144 square meters of paint.

2. The Greater Number

Read two integers X and Y and report which is greater (or that they are equal).

Enter X: 4
Enter Y: 6
Y is greater

3. Quotient

Read two integers X and Y, and print their quotient if X is exactly divisible by Y, or "indivisible" otherwise.

Enter X: 8
Enter Y: 2
8 divided by 2 is 4
===
Enter X: 10
Enter Y: 3
indivisible

4. Largest of Three

Read three integers on a single line, and print out the largest of them.

Enter 3 numbers: 4 17 2
The largest is 17.

5. Which Decade

Read a year from the console. Write the decade that it was in.

Year: 1995
1995 was in the 1990s
===
Year: 1821
1821 was in the 1820s
===
Year: 2010
2010 was in the 2010s

6. Oranges

Ask the user for a number N, then write the word "orange" N times.

How many oranges: 4
orange
orange
orange
orange

7. Numbers from 2 to 20

Print the even numbers from 2 to 20 on the console. (Don't use a separate writeln statement for each number!)

2
4
6
8
10
12
14
16
18
20

8. Square and Cubes

Read a number N and print a table of the squares and cubes of all integers from 1 to N.

Enter N: 6
1^2 = 1, 1^3 = 1
2^2 = 4, 2^3 = 8
3^2 = 9, 3^3 = 27
4^2 = 16, 4^3 = 64
5^2 = 25, 5^3 = 125
6^2 = 36, 6^3 = 216

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

10. Factorial

Write a program that reads a number N and prints the value of N!, i.e. 1 ⋅ 2 ⋅ … ⋅ N.

Enter N: 6
6! = 720

11. Exponentiation

Write a program that reads two values A and B and prints the value of AB. Implement exponentiation using repeated multiplication.

12. All Divisors

Write a progran that reads a number N and prints all of its divisors, as well as a count of the divisors.

Enter N: 12
1
2
3
4
6
12
There are 6 divisors

13. Leap Years

Read a year from the console, and write out "leap" if it is a leap year, or "no leap" if it isn't.

A year is a leap year if it's divisible by 4, unless it's divisible by 100, in which case it isn't a leap year, unless it's divisible by 400, in which case it is actually a leap year.

Enter year: 1922
no leap
===
Enter year: 1900
no leap
===
Enter year: 2000
leap
===
Enter year: 2020
leap

14. 20 Minutes Later

Read two numbers on the same line representing a 24-hour time in hours and minutes. Print the time that is 20 minutes later, wrapping past midnight if necessary.

Time: 2 30
2:50
===
Time: 5 50
6:10
===
Time: 23 55
0:15

15. N Minutes Later

Read two numbers on the same line representing a 24-hour time in hours and minutes. Read another number representing an offset N in minutes (a positive number). Print the time that is N minutes later, wrapping past midnight if necessary.

Time: 2 30
Offset: 20
2:50
===
Time: 5 50
Offset: 100
7:30
===
Time: 23 55
Offset: 1000
16:35

16. Primality Test

Write a program that reads a number N and prints "prime" if N is prime, "not prime" otherwise.

Enter N: 19
prime
===
Enter N: 91
not prime