The modulo is the remainder of the whole division. Let's take the example of the division of 618 by 4 :

The result of the integer division of 618 by 4 is 154. The remainder of the integer division is **2**.

**Note:** the operands of the modulo must be **integers**.

The modulo is a very common operator in computer science. It allows for example to test if a number is even or odd :

- if the number is even, the remainder of the division by 2 is zero;
- if the number is odd, the remainder of the division by 2 is one.

Here is an example of a program that tests the parity of the variable `number`

(we will
come back to this in detail later):

```
// Check if the number is even or odd
if (number % 2 == 0)
printf ("It is an even number");
else
printf ("It is an odd number");
```

We want to build a fence of 12345 centimeters. This fence is made with panels of 125 centimeters. Complete the program below to calculate the length that will remain to be fenced after the panels are installed.

```
#include <stdio.h>
int main(void) {
int length = 12345;
int pannels = 125;
int remainder;
// Calculate the remainder to be fenced
// COMPLETE HERE
printf ("%d cm left to fence.\n", remainder);
return 0;
}
```

**What is the modulo operator ( %)?**

Check
Bravo! The modulo operator calculates the remainder of the integer division.
Try again...

**What can the modulo operator be used for?**

Check
Bravo ! The remainder of the division informs us about the divisibility of the operands.
Try again...

**What will the variable x contain after the next line?**

`x = 12 % 4 ;`

Check
Bravo! 12 being divisible by 4, the remainder is 0.
Try again...

**What can the variable x contain?**

`x = b % 3 ;`

Check
Bravo! The remainder of the division by 3 can only be 0, 1 or 2.
Try again...

**How to isolate the number of units of the variable x?**

Check
Bravo! The remainder of the division by 10 will give the number of units.
Try again...

**How to isolate the tens digit of the variable x?**

Check
Bravo! There are indeed two solutions.
Try again...

- Learn C programming
- Lesson 1.1. History of C programming language
- Lesson 1.2. First program
- Lesson 1.3. Compilation
- Lesson 1.4. Preprocessor directives
- Lesson 1.5. Which compiler to choose?
- Lesson 1.6. Flowchart
- Lesson 2.1. Types and variables
- Lesson 2.2. Integers
- Lesson 2.3. Floating point numbers
- Lesson 2.4. Characters
- Lesson 2.5. Variables initialization
- Lesson 2.6. Ariane flight 501
- Lesson 3.1. Arithmetic operators
- Lesson 3.3. Operators and types
- Lesson 3.4. Type casting
- Lesson 3.5. Bitwise operators
- Lesson 3.6. Bitwise operators details
- Lesson 3.7. Shifting operators
- Lesson 3.8. Assignment operators
- Lesson 3.9. Increment and decrement operators
- Lesson 3.10. Comparison operators
- Lesson 3.11. Logical operators
- Lesson 3.12. Operator precedence
- Lesson 4.1. printf
- Lesson 4.2. scanf
- Lesson 4.3. putchar
- Lesson 5.1. Conditional branching (if...else)
- Lesson 5.2. Nested if and indentation
- Lesson 5.3. Checking intervals
- Lesson 5.4. Ternary Operator ?:
- Lesson 5.5. switch..case statement
- Lesson 5.6. Break keyword in switch..case
- Lesson 6.1. do..while loop in C
- Lesson 6.2. While loop in C
- Lesson 6.3. For loop in C
- Lesson 6.4. How to choose the right loop in C ?
- Lesson 6.5. Exercices on loops in C
- Lesson 7.1. Bit masking
- Lesson 7.2. Bit masking, how to clear a bit in C?
- Lesson 7.3. Bit masking, how to set a bit in C?
- Lesson 7.4. Bit masking, how to toggle a bit in C?
- Lesson 7.5. Bit masking, how to check a single bit in C?
- Lesson 7.6. Overview of bit masking
- Lesson 8.1. Basic syntax of C functions
- Lesson 8.2. Calling C functions
- Lesson 8.3. Void keyword
- Lesson 8.4. Return keyword
- Lesson 8.5. Variable scope
- Lesson 8.6. Global variables
- Lesson 8.7. Static variables
- Lesson 8.8. Random numbers in C
- Lesson 8.9. Mathematical functions in C
- Lesson 9.1. Syntax of C arrays
- Lesson 9.2. Array initialization
- Lesson 9.3. Multidimensional arrays
- Lesson 9.4. How C arrays are stored in memory?
- Lesson 9.5. Arrays and functions
- Lesson 9.6. Exercises on C arrays
- Lesson 10.1. Strings in C
- Lesson 10.2. Null terminated string
- Lesson 10.3. The string.h library
- Lesson 10.4. String and functions
- Lesson 11.1. Introduction to pointers in C
- Lesson 11.2. Syntax of C pointers
- Lesson 11.3. Dynamic memory allocation
- Lesson 11.4. Incrementing pointers
- Lesson 11.5. Pointers as function argument in C
- Lesson 12.1. Introduction to structures in C
- Lesson 12.2. Properties of structures in C
- Lesson 12.3. Structures and pointers
- Lesson 12.4. Structures and functions
- Lesson 13.1. Recursive functions in C
- Lesson 13.2. Depth of recursive functions
- Lesson 13.2. Mutual recursion
- Lesson 14.1. Additional exercises

Last update : 09/19/2023