Unit 5: Numbers & Patterns
Remainders and shapes.
Unit 5 of 11 in C programming for kids. Its 4 lessons are Leftovers, Shortcuts, Loops Inside Loops and Number Cruncher — below is everything each one explains, and a question or two from it to try.
Every sample on this page was compiled and run with gcc before it shipped, and prints exactly what it says it prints.
This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.
🍕 Leftovers
What is left over?
Share 7 slices between 2 people and 1 is left over. % gives you that leftover — the remainder. You met / throwing the remainder away; % keeps only the remainder.
C
printf("%d\n", 7 / 2);
printf("%d\n", 7 % 2);
It prints
3 1
The even-or-odd trick
A leftover of 0 means it divided perfectly, so n % 2 == 0 is the neatest way to ask "is this even?" — a trick you will use for the rest of your life.
C
int n = 6;
if (n % 2 == 0) {
printf("even\n");
} else {
printf("odd\n");
}
It prints
even
Try it yourself
What does this print?
C
printf("%d\n", 9 % 4);
Which one is true only for odd numbers?
- n % 2 == 0
- n % 2 == 1
- n / 2 == 0
- n % 1 == 0
Answer them in the app
⚡ Shortcuts
Adding one, the short way
You have been writing i = i + 1. C has i++ for exactly that, and i-- to take one away. You already use them in every for loop.
C
int score = 5;
score++;
printf("%d\n", score);
score--;
score--;
printf("%d\n", score);
It prints
6 4
Changing by any amount
+= means "add this on". There is a version for each operation, and they save a lot of repeating the name.
C
int n = 10;
n += 5;
printf("%d\n", n);
n *= 2;
printf("%d\n", n);
n /= 3;
printf("%d\n", n);
It prints
15 30 10
Try it yourself
What does this print?
C
int total = 0;
for (int i = 1; i <= 4; i++) {
total += i;
}
printf("%d\n", total);
What is total *= 3 the same as?
- total = 3
- total = total * 3
- total = total + 3
- total * 3
Answer them in the app
🔳 Loops Inside Loops
A loop inside a loop
Put a loop inside another and the inner one runs all the way through every single time the outer one turns. Two turns outside × three inside = six.
C
for (int row = 0; row < 2; row++) {
for (int star = 0; star < 3; star++) {
printf("*");
}
printf("\n");
}
It prints
*** ***
Growing rows
Let the inner loop depend on the outer one and the shape changes as you go — the usual way to draw a triangle.
C
for (int row = 1; row <= 3; row++) {
for (int star = 0; star < row; star++) {
printf("#");
}
printf("\n");
}
It prints
# ## ###
Try it yourself
What does this print?
C
int count = 0;
for (int a = 0; a < 3; a++) {
for (int b = 0; b < 4; b++) {
count++;
}
}
printf("%d\n", count);
Why is the bare printf("\n"); outside the inner loop?
- It looks nicer
- It ends the row once, after all that row’s stars are printed
- It is a mistake
- It makes the loop faster
Answer them in the app
🏆 Number Cruncher
Try it yourself
What does this print?
C
printf("%d %d\n", 17 % 5, 17 / 5);
What does this print?
C
int total = 0;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
total += i;
}
}
printf("%d\n", total);
Answer them in the app