Search⌘ K
AI Features

Discussion: Eenie, Meenie, Miney, Mod

Explore the modulo operator in C to understand how it returns remainders and counts intervals. Learn to correctly use the operator to produce predictable output and avoid errors such as division by zero, enhancing your coding reliability.

Run the code

Now, it's time to execute the code and observe the output.

C
#include <stdio.h>
int main()
{
int m = 1;
while( m<10 ) {
if( 2%m )
printf("%d - Odd\n", m);
else
printf("%d - Even\n", m);
m++;
}
return(0);
}

Modulo operator mantra

The modulo operator (%) returns the remainder of the first number divided by the second: 5 % 3 results in two, the remainder of 5 ÷ 3. While such expressions are interesting, ...