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. Oranges

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

How many oranges: 4
orange
orange
orange
orange

2. 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

3. 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.

4. 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

5. 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

6. 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.

7. 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

8. Square

Read a number N, then print an N x N square of asterisks.

Enter N: 5
*****
*****
*****
*****
*****

9. Triangle

Read a number N, then print an N x N triangle of asterisks.

Enter N: 6
*
**
***
****
*****
******

10. The Ell

Read a number N, then use asterisks to print a letter L with height and width N, and a thickness of 3.

Size: 7
***
***
***
***
*******
*******
*******

11. Average

Read a set of floating-point numbers from the console until the user presses ctrl+D (Unix, macOS) or ctrl-Z (Windows). Print their average.

2.5
3.5
5.5
6.5
^D
4.500000000E+00

12. Rolling N Dice

Read a number N from the console. Simulate rolling N 6-sided dice. Write the value of each die roll, followed by the sum of all of them.

How many dice? 4
6 2 6 1
total: 15
===
How many dice? 2
6 2
total: 8

13. All Even

Read a set of integers from the console until the user presses ctrl+D or ctrl+Z. Print "all even" if they are all even, or "some odd" otherwise.

6
8
10
^D
all even
===
6
8
9
^D
some odd

14. While and Repeat

Here is a loop written using for:

var
  i: integer;
begin
  for i := 1 to 5 do
    writeln(i);
end

Write the same loop using while instead of for. Now write the same loop again using repeat. Since you are looping twice, your output should now look like this:

1
2
3
4
5
1
2
3
4
5

15. Password

Read a password P, then clear the screen and repeatedly read password guesses until the password P is entered.

Hint: You can use the function clrScr from the crt unit to clear the screen. Write 'uses crt;' at the beginning of the program to import this unit. The function takes no parameters, so you can invoke it simply like this: clrScr;

Enter password: pineapple
(screen clears)
Password: apple
wrong!
Password: potato
wrong!
Password: pineapple
correct!

16. 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

17. Positive and Negative

Read integers from the console until the user presses ctrl+D or ctrl+Z. Write the sum of the positive integers and the sum of the negative integers.

Enter numbers:
4
2
-6
-1
5
-2
^D
Positive sum: 11
Negative sum: -9

18. Equal To The First

Read integers from the console until the user presses ctrl+D or ctrl+Z. Write a number indicating how many of the integers were equal to the first one (not including the first one itself).

Enter numbers:
4
2
2
8
4
4
3
^D
2 numbers were equal to 4
===
Enter numbers:
5
3
6
^D
0 numbers were equal to 5

19. Largest and Smallest

Read integers from the console until the user presses ctrl+D or ctrl+Z. Write the largest and the smallest of the integers.

Enter numbers:
4
6
2
8
10
5
^D
The largest was 10.
The smallest was 2.

19. Multiples of 3 and 5

Solve Project Euler's problem 1.

20. Even Fibonacci numbers

Solve Project Euler's problem 2.

21. Flipping a Coin

Simulate coin flips, printing an 'H' each time the coin falls on heads and 'T' each time it falls on tails. Once you reach the third H, stop and write how many coins were flipped.

HTTTHTH
7 flips
===
TTTHTTTTTHH
11 flips

22. Reverse It

Read a string and print it backwards without using the library function reverseString.

Enter string: zyzzyva
avyzzyz

23. Spaces In Between

Read a string and write it out with a space between each adjacent pair of characters.

Enter string: waffle
w a f f l e

24. Double Or Nothing

Read a string and print 'double' if any two adjacent characters are the same, or 'nothing' otherwise.

Enter string: abacuses
nothing
===
Enter string: lionesses
double

25. Number Of O's

Read a string. Print out the number of uppercase or lowercase O's in the string.

Enter string: noodle
2
===
Enter string: The Original Octopi
3 

26. Power of Two

Read an integer. Print "pow" if it is a power of two, "no" otherwise. (Note: 20 = 1).

Enter number: 64
pow
===
Enter number: 68
no
===
Enter number: 256
pow
===
Enter number: 1
pow

27. 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

28. 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

29. 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

30. 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. 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