How to make sense of modulo in c

The modulo operator in C will give the remainder that is left over when one number is divided by another. For example, 23 % 4 will result in 3 since 23 is not evenly divisible by 4, and a remainder of 3 is left over.

If you want to output whether or not a number is divisible by 4, you need to output something other than just the mod result. Essentially, if mod = 0 than you know that one number is divisible by another.

If you want to output whether or not the number is divisible by 4, I would suggest creating a new character that is set to “y” (yes) or “n” (no) depending on the result of the mod operation. Below is one possible implementation to generate a more meaningful output:

#include <stdio.h>
#include <ctype.h>
#include <math.h>

int main()
{
    int my_input[] = {23, 22, 21, 20, 19, 18};
    int n, mod;
    char is_divisible;
    int nbr_items = sizeof(my_input) / sizeof(my_input[0]);

    for (n = 0; n < nbr_items; n++)
    {
        mod = my_input[n] % 4;
        is_divisible = (mod == 0) ? 'y' : 'n';
        printf("%d modulo %d --> %c\n", my_input[n], 4, is_divisible);
    }
}

This will give the following:

23 modulo 4 --> n
22 modulo 4 --> n
21 modulo 4 --> n
20 modulo 4 --> y
19 modulo 4 --> n
18 modulo 4 --> n

Leave a Comment

deneme bonusu veren sitelerbahis casinomakrobetceltabetpinbahispolobetpolobet girişpinbahis girişmakrobet girişpulibet girişmobilbahis girişkolaybet giriş