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.

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...
answer is 1

Answer is 3 ..

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

I got your doubt

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..

Edited 6 Years Ago by Shankye: n/a

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

Edited 6 Years Ago by ankitcuul: n/a

This article has been dead for over six months. Start a new discussion instead.