How to use mod in C C++?

Asked

Viewed 57,581 times

5

how do I use the MOD function in C C++? (N MOD 2) = 0 I have to use this line of my code.

1 answer

12


The MOD operator is the %.

int n = 40;
if (n % 2 == 0) {
    cout << num << " é par";
}

Additionally see std::modulus

  • [Error] invalid operands to Binary % (have 'float' and 'int') now giving this error could explain me why?

  • 1

    Why the rest of the division operator is for operations with integer numbers and you are trying to do this with a float. In the case of floating numbers this operator makes no sense, the division of a float or double will leave no remains (although the result is not accurate due to how these types work, a float divided by a float or a int there’s always rest 0).

  • 1

    And when the operand is less than the operator, ex: 1 % 2 != 0?

  • @Robson, think about mod as the rest of a division. If you split a number n for a value greater than n the entire division will be 0 and P rest (module) will be n.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.