Thursday, January 16, 2025
HomeProgrammingModulo Operator (%) in C/C++ with Examples

Modulo Operator (%) in C/C++ with Examples

The modulo operator (%) in C/C++ returns the remainder of a division between two integers. It is useful for operations where the remainder is needed, such as determining even/odd numbers or cyclic operations.

Syntax:

\text{result} = \text{a} \% \text{b};

See also  Can someone explain SSH tunnel in a simple way?

Examples:

1. Example 1:

10 \% 3 = 1

2. Example 2:

8 \% 2 = 0

Use Cases:

Even/Odd Check:

\text{if} (\text{num} \% 2 == 0) \text{ // num is even}

The modulo operator is only applicable to integer types, and the divisor (b) must not be zero to avoid a runtime error.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x