-6

Why do I get this output: 0 1 2 3 0 1 2 3 0 1 after running the code below? Doesn't the modulus operation finds the remainder after division of one number by another?

#include <iostream>
using namespace std;
int main ()
{
for (int i=0; i< 10; ++i)
cout << i % 4 << " ";
}
Jack
  • 11
  • 5
  • 3
    Yes, that's how modulus works. – bejado Mar 03 '17 at 06:11
  • 3
    **−1** What research did you do before asking? – Cheers and hth. - Alf Mar 03 '17 at 06:25
  • Googled it..... – Jack Mar 03 '17 at 06:38
  • 1
    And what would you expect the output to be? Why do you think the output you're getting is not the remainder? – Angew is no longer proud of SO Mar 03 '17 at 07:54
  • I still don't understand How they explained it here.. Should 't I have the output: 0 25 50 75 0 25 50 75 0... And so on till i is 10? Because: 0/4=0; 1/4= 0,25; 2/4=0.50...and so on.. – Jack Mar 03 '17 at 07:57
  • 1
    @Jack: This is integer arithmetic, in particular it's [integer division](http://mathworld.wolfram.com/IntegerDivision.html). In C++, as opposed to the definition in Mathworld, and as opposed to the Python language's integer division, in C++ integer division rounds towards zero. – Cheers and hth. - Alf Mar 03 '17 at 08:37
  • So 0.25 round to zero should be zero, right? 0,50 rounded to zero should be also zero.. So the first 3 digits as output from the code should be : 0 0 0 , not 0 1 2 – Jack Mar 03 '17 at 09:53
  • Now i get it, but only from 5... 5: 4*1 = 4 so 5-4=1 .. Also for 6: 4*1= 4 and 6-4 = 2 and so on.. That's the logic I find. But only when the left term is higher than 4... – Jack Mar 03 '17 at 09:56
  • It is difficult to offer solutions when the problem statement is simply, "it doesn't work". Please [edit] your question to give a more complete description of what you expected to happen and how that differs from the actual results. See [ask] for hints on what makes a good explanation. – Toby Speight Mar 03 '17 at 13:22
  • 1
    Your problem is not related to C++ or programming. It is related to math. You don't know what the math term "remainder" means. Look it up. – Benjamin Lindley Mar 03 '17 at 14:30

4 Answers4

4

The answer is correct. '%' mean "reminder". The % operator is remainder operator. The A % B operator actually answer the question “If I divided A by B using integer arithmetic, what would the remainder be?”

dividend = quotient * divisor + remainder

0 % 4 = 0
1 % 4 = 1
2 % 4 = 2
3 % 4 = 3
4 % 4 = 0
5 % 4 = 1
.....
etc..

For negative number...

   1 % (-4) = 1
(-2) % 4    = -2
(-3) % (-4) = -3

With a remainder operator, the sign of the result is the same as the sign of the dividend

you can read more at What's the difference between “mod” and “remainder”?

Community
  • 1
  • 1
roottraveller
  • 7,942
  • 7
  • 60
  • 65
1

Yes, that's how modulus works. The output is correct.

0 % 4 = 0
1 % 4 = 1
2 % 4 = 2
3 % 4 = 3
4 % 4 = 0
5 % 4 = 1
...

Take the number, remove as many 4's as you can. Whatever is left over is the modulus.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
bejado
  • 1,430
  • 12
  • 19
1

Modulus operator returns the remainder after dividing the first number with the second one.

0 % 4 = 0
1 % 4 = 1
2 % 4 = 2
3 % 4 = 3
4 % 4 = 0
5 % 4 = 1
6 % 4 = 2
7 % 4 = 3
8 % 4 = 0
9 % 4 = 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
msc
  • 33,420
  • 29
  • 119
  • 214
1

It does.
0 / 4 = 0 remainder 0
1 / 4 = 0 remainder 1
and so on.

MotKohn
  • 3,485
  • 1
  • 24
  • 41