Week 2: Exercises

1. Square

Read a number N, then print an N x N square of asterisks as in the example below.

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

2. Triangle

Read a number N, then print an N x N triangle of asterisks as in the example below.

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

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

4. 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, with two digits after the decimal point.

2.5
3.5
5.5
6.5
^D
4.50

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

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

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

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

9. Number Guessing

Write a program that plays the following game: the user thinks of a number from 1 to 1000 and the computer guesses it. (This is the same game we wrote in the lab, but with the roles reversed.)

Think of a number from 1 to 1000.
My guess: 500.  Is this (h)igh, (l)ow, or (c)orrect?  h
My guess: 250.  Is this (h)igh, (l)ow, or (c)orrect?  l
…

My guess: 467.  Is this (h)igh, (l)ow, or (c)orrect?  c

Total guesses: 8

10. Multiples of 3 and 5

(Project Euler, problem 1)

Compute the sum of all positive integers N < 1000 such that N is a multiple of 3 and/or a multiple of 5.

11. Fibonacci Numbers

The sequence of Fibonacci numbers Fi is defined as follows: F1 = 1, F2 = 1, and Fi = Fi-1 + Fi-2 for all i ≥ 3. So the sequence is

1, 1, 2, 3, 5, 8, 13, 21, …

Write a program that reads an integer N and writes the first N numbers in the Fibonacci sequence.

Enter N: 8
1 1 2 3 5 8 13 21

12. Sum of Even Fibonacci Numbers

(Project Euler, problem 2)

Determine the sum of all even Fibonacci numbers that are less than 4,000,000.

13. Second Largest

Write a program that reads a series of integers from standard input until the user presses Ctrl+D or Ctrl+Z. The program should then print the second largest of the integers that were read.

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

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

16. Linear Equations

Write a program that reads the values of (x + y) and (x – y), where x and y are unknown real values. The program should solve for x and y and print their values, with one digit after the decimal point.

Enter x + y: 8.9
Enter x – y: 1.7
x = 5.3
y = 3.6

17. Non-Linear Equations

Write a program that reads the values of (x + y) and (xy), where x and y are unknown real values. The program should solve for x and y and print their values, with one digit after the decimal point. If no solution is possible, write "No solution".

Enter x + y: 8
Enter xy: 15
x = 3
y = 5
===
Enter x + y: 1
Enter xy: 2
No solution