2

i am not understanding this modulo in c languge.
For example:

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

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

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

Gives:

23 modulo 4 --> 3
22 modulo 4 --> 2
21 modulo 4 --> 1
20 modulo 4 --> 0
19 modulo 4 --> 3
18 modulo 4 --> 2

I would have expected a number that i can make sense of.
Essentially i am trying to test if a number is divisible by 4.

5
  • 3
    If a number is divisble by X, the modulo (which is the remainder of the division) is 0. In your case, 20 modulo 4 is 0, thus divisible by 4. What is it that you're not understanding?
    – AntonH
    Apr 9, 2017 at 3:30
  • thanks. Why 23 modulo 4 is 3 ? Why 22 modulo 4 is 2 ? etc.
    – Cy Bu
    Apr 9, 2017 at 3:34
  • 1
    23 = 4x5 + 3. So the result of 23 modulo 4 is 3. 22 = 4x5 + 2. So the result of 22 modulo 4 is 2. 19 = 4x4 + 3. So the result of 19 modulo 4 is 3. And so on. Did you read up on what a modulo, and what a remainder, is?
    – AntonH
    Apr 9, 2017 at 3:36
  • 4
    This has nothing to do with C. Google "how does modulo work" for dozens of millions of resources that'll explain every detail you want to know. It's a mathematical operator.
    – Nic
    Apr 10, 2017 at 3:40
  • 4
    It should be noted that in c "%" is neither modulo nor remainder, due to the behaviour with negative numbers not matching the mathematically defined definitions. E.g. in c 5%(-3)=2 and (-5)%3=-2 Feb 12, 2019 at 0:14

2 Answers 2

8

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
1
  • 2
    I think you mean you ternary operation to be is_divisible = mod == 0 ? 'y' : 'n'?
    – cajomar
    Aug 16, 2020 at 15:01
6

I'm sure we know the basic division equation from high school math

dividend = divisor*quotient + remainder

Now:
1. The "/" operator gives us the quotient.
2. The "%" operator gives us the remainder

example:

    say a = 23, b = 4
    a / b = 23 / 4 = 5
    a % b = 23 % 4 = 3

    23 = 4*5 + 3 

Here 4 is the quotient and 3 is the remainder.

If a number is perfectly divisible by a divisor, then remainder is zero.

So:

    20/4 = 5 (quotient)
    20%4 = 0 (remainder)

To test if a no if divisible by 4, the check should be something like if (num % 4 == 0).
Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.