+ 3

How % operator behaves in "c" and "python"

provide some relevant stuff with some examples as well

26th Nov 2017, 10:29 AM
sai chander
sai chander - avatar
4 Answers
+ 2
Well, let's say you have -5 % 3; Java and C would give -2. (Negative remainder of 5/3) Python, on the other hand, would give 1. (Python adds 3 until the answer is positive). Let's say 13 % -2, on the other hand. Java and C would give you 1 (13 % 2. They ignore negative denominators.) Python, however, would give you -1. (Negative remainder of 13 / 2. Basically, Python keeps subtracting 2 until the number is negative.) Hope this is clear and it helped. šŸ˜‰ (No idea why I talked about Java here, but just in case anyone wants to know. šŸ˜‰)
26th Nov 2017, 11:58 AM
blackcat1111
blackcat1111 - avatar
+ 2
The modulo operator takes the remainder of a division between two numbers. For example, 11 % 3 = 2, because the remainder of 11/3 is 2. (Or at least, I assume the % operator is the modulo operator in C, because I don't know C. šŸ˜‰ But that's the way it operates in literally every programming language I know, so it should be that way.)
26th Nov 2017, 10:42 AM
blackcat1111
blackcat1111 - avatar
+ 2
Actually, % in C(++) and python are different For example, take -8 % 5 In python this would print 2, in C(++) this would print -3. In python % is implemented as mod, meaning -8 + 5 * 2 = 2 in C(++) % is implemented as the remainder -8 / 5 = -1 with a remainder of -3 For positive numbers these don't make a difference, but you should be aware of this when dealing with negative numbers. Edit: Wrote this before the actual negative question popped up ^^
26th Nov 2017, 11:59 AM
Dennis
Dennis - avatar
+ 1
guys what if either numerator or denominator is negative? will c or python gives the same answer or different. just check it once. example: 2%3=2 what if numerator or denominator is negative
26th Nov 2017, 11:52 AM
sai chander
sai chander - avatar