Hmm I was lookin through a recursion program in this book i have here and the only thing I'm having trouble with is the modulus operator.

I know that it returns the remainder of 2 numbers.

But, suppose I have this: x % y

If x > y then the result is x. Am I tripping out o_O?

If x > y then the result is x. Am I tripping out o_O?

if x is 6 and y is 2, then the result is 0.

i'm sorry i meant if x < y.

Yes, then result will be x.

I think you've misunderstood how modulus works, or maybe I've misunderstood you :P

Basically in your x % y example, you are taking y away from x until x is less than y, then returning x.

Example in C that I wrote a while ago:

``````
``````

## All 10 Replies

If x > y then the result is x. Am I tripping out o_O?

if x is 6 and y is 2, then the result is 0.

i'm sorry i meant if x < y.

i'm sorry i meant if x < y.

Yes, then result will be x.

Yes I have also noticed this its is true that if x<y then x%y=x but if x=1 and y=8 then shouldn't the remainder be 2

I think you've misunderstood how modulus works, or maybe I've misunderstood you :P

Basically in your x % y example, you are taking y away from x until x is less than y, then returning x.

Example in C that I wrote a while ago:

``````int mod(int a, int b)
{
for(; a > b; a -= b);

return a;
}``````

So if x = 1, y = 8

if 1 > 8 we take away 8, but it isn't so...

Then tell me what would be 3*3*3%4?

First 3*3*3 gives 27
thn modulus operator gives remainder of 27%4
Hence ans is 3..

U thinking

3*3*3%4
= 3*3*(3%4)
= 3*3*3
= 27

but its wrong ..

U need to refer operator precedence in C

* / % Multiplication/division/modulus left-to-right
so first multiplication is done and thn division if any and thn modulus operator comes..
Hence the result 3..

ya i agree with Shankye
As u already know that modulus operator gives the remainder of the division of two operands ...!!...n in case two operands are such that
x<y then result is x
let's proove this thing
u might have notinced this that the modulus operator gives same result for the operation (x%y) if x is such that,
x=a+ny
n=0,1,2,3,4....
thus for this equation to be true...!!
x%y=x for x<y
HOPE THIS HELPS U

Check this
precedence of all operators ..

Problem solved may be ..
Mark it solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.