Lesson 6.5. Exercices on loops in C

Exercise 1

Write a program that displays all the combinations of two 6-sided dice:

die1=1 die2=1
die1=1 die2=2
die1=1 die2=3
die1=1 die2=4
die1=1 die2=5
die1=1 die2=6
die1=2 die2=1
die1=2 die2=2
die1=2 die2=3
...

Exercise 2

Write a program that displays the ASCII table from 32 to 127. For each character, the program displays the ASCII code in decimal and hexadecimal:

┏━━━━━━━━━━━━━━━━┓
┃    ASCII TABLE    ┃
┗━━━━━━━━━━━━━━━━┛
| DEC | HEX | ASCII |
|-----|-----|-------|
|  32 |  20 |       |
|  33 |  21 |   !   |
|  34 |  22 |   "   |
|  35 |  23 |   #   |
|  36 |  24 |   $   |
|  37 |  25 |   %   |
|  38 |  26 |   &   |
|  39 |  27 |   '   |
|  40 |  28 |   (   |
|  41 |  29 |   )   |
|  42 |  2a |   *   |
|  43 |  2b |   +   |
|  44 |  2c |   ,   |

Exercise 3

Write a program that displays a rectangle of side WIDTH x HEIGHT with characters. WIDTH and HEIGHT are constants :

#define   WIDTH     16
#define   HEIGHT    3

Here is an example with the values defined above:

***************
***************
***************

Exercise 4

Write a program that displays a triangle according to the example below:

*******
 ******
  *****
   ****
    ***
     **
      *

The size of the triangle is defined by a constant WIDTH. In the example above, WIDTH is 7.

Exercise 5

Write a program that displays all the divisors of an integer entered by the user:

Enter a positive number: 30
1 is a divisor of 30
2 is a divisor of 30
3 is a divisor of 30
5 is a divisor of 30
6 is a divisor of 30
10 is a divisor of 30
15 is a divisor of 30
30 is a divisor of 30

Exercise 6

Write a program that displays all the possible draws of the lotto (5 balls numbered from 1 to 49). Be careful, it is not a non-repeating draw, each ball can appear only once in each draw.

 1  2  3  4  5
 1  2  3  4  6
 1  2  3  4  7
 1  2  3  4  8
 1  2  3  4  9
 1  2  3  4 10
 1  2  3  4 11
 1  2  3  4 12
 1  2  3  4 13
 1  2  3  4 14
 1  2  3  4 15
 1  2  3  4 16
 1  2  3  4 17
 1  2  3  4 18
 1  2  3  4 19
 1  2  3  4 20
 1  2  3  4 21
 1  2  3  4 22
 1  2  3  4 23

Do you realize that if this program works properly, it will display the next winning lottery draw? 😁 🤑

Exercise 7

Fizz-Buzz is a game in which players take turns counting. But when a player has to say a multiple of 3, they replace it with Fizz. If it's a multiple of 5, they replace it with Buzz. If it is a multiple of 3 and 5, he must say Fizz-Buzz. Write a program that displays the sequence of the game for the first 100 numbers:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
Fizz-Buzz
16
17

This exercise is a test used in job interviews. It allows to estimate the ability of a developer to code cleanly. There are several ways to program this game. In the answer key, readability has been chosen, but this solution is not optimal because it repeats the same tests several times. The following video details the evolution from a naive solution to a more elegant solution. Note that the final solution in the video is not C-compliant. However, the thought process is still interesting.

Exercise 8

Write a program that asks the user to enter a positive integer. The program then displays this number in Roman numerals:

Enter a positive integer: 2022
2022 is written MMXXII in Roman numerals.

The solution presented below was written by Maxime JOMEAU, whom I thank for his work.

See also


Last update : 11/22/2022