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
...
#include <stdio.h>

int main(void) {
    int die1, die2;

    // For each value of the first die
    for (die1 = 1; die1 <= 6; die1++)
        // For each value of the second die
        for (die2 = 1; die2 <= 6; die2++)
            printf("die1=%d die2=%d\n", die1, die2);

    return 0;
}

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 |   ,   |
#include <stdio.h>

int main(void) {
    int i;

    // Header
    printf("┏━━━━━━━━━━━━━━━━━━━┓\n");
    printf("┃    ASCII TABLE    ┃\n");
    printf("┗━━━━━━━━━━━━━━━━━━━┛\n");
    printf("| DEC | HEX | ASCII |\n");
    printf("|-----|-----|-------|\n");

    // One line per character
    for (i = 32; i <= 127; i++)
        printf("| %3d | %3x |   %c   |\n", i, i, i);

    return 0;
}

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:

***************
***************
***************
#include <stdio.h>

#define   WIDTH     16
#define   HEIGHT    3

int main(void) {
    int x, y;

    // For each line
    for (y = 0; y < HEIGHT; y++) {
        // Display WIDTH stars
        for (x = 0; x < WIDTH; x++)
            putchar('*');
        putchar('\n');
    }

    return 0;
}

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.

#include <stdio.h>

#define   WIDTH     7

int main(void) {
    int line, i;

    for (line = 0; line < WIDTH; line++) {
        // Spaces before the stars
        for (i = 0; i < line; i++)
            putchar(' ');
        // Stars
        for (i = line; i < WIDTH; i++)
            putchar('*');
        putchar('\n');
    }

    return 0;
}

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
#include <stdio.h>

int main(void) {
    unsigned int n, i;

    // Ask for a positive number
    printf("Enter a positive number: ");
    scanf("%u", &n);

    // Test each integer between 1 and n
    for (i = 1; i <= n; i++)
        if (n % i == 0)
            printf("%u is a divisor of %u\n", i, n);

    return 0;
}

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? 😁 🤑

#include <stdio.h>

int main(void) {
    int b1, b2, b3, b4, b5;

    // Each ball is greater than the previous one,
    // so a ball cannot appear twice and each draw is displayed once
    for (b1 = 1; b1 <= 49; b1++)
        for (b2 = b1 + 1; b2 <= 49; b2++)
            for (b3 = b2 + 1; b3 <= 49; b3++)
                for (b4 = b3 + 1; b4 <= 49; b4++)
                    for (b5 = b4 + 1; b5 <= 49; b5++)
                        printf("%2d %2d %2d %2d %2d\n", b1, b2, b3, b4, b5);

    return 0;
}

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
#include <stdio.h>

int main(void) {
    int i;

    for (i = 1; i <= 100; i++) {
        if (i % 3 == 0 && i % 5 == 0)
            printf("Fizz-Buzz\n");
        else if (i % 3 == 0)
            printf("Fizz\n");
        else if (i % 5 == 0)
            printf("Buzz\n");
        else
            printf("%d\n", i);
    }

    return 0;
}

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.

#include <stdio.h>

int main(void) {
    unsigned int n, rest;

    // Ask for a positive integer
    printf("Enter a positive integer: ");
    scanf("%u", &n);

    printf("%u is written ", n);
    rest = n;

    // Thousands
    while (rest >= 1000) { printf("M"); rest -= 1000; }

    // Hundreds
    if (rest >= 900) { printf("CM"); rest -= 900; }
    if (rest >= 500) { printf("D"); rest -= 500; }
    if (rest >= 400) { printf("CD"); rest -= 400; }
    while (rest >= 100) { printf("C"); rest -= 100; }

    // Tens
    if (rest >= 90) { printf("XC"); rest -= 90; }
    if (rest >= 50) { printf("L"); rest -= 50; }
    if (rest >= 40) { printf("XL"); rest -= 40; }
    while (rest >= 10) { printf("X"); rest -= 10; }

    // Units
    if (rest >= 9) { printf("IX"); rest -= 9; }
    if (rest >= 5) { printf("V"); rest -= 5; }
    if (rest >= 4) { printf("IV"); rest -= 4; }
    while (rest >= 1) { printf("I"); rest -= 1; }

    printf(" in Roman numerals.\n");

    return 0;
}

See also


Last update : 11/30/2026