944,204 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 26349
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 13th, 2006
-1

modulus operator %

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
Apr 13th, 2006
1

Re: modulus operator %

Quote originally posted by degamer106 ...
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.
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Apr 13th, 2006
0

Re: modulus operator %

i'm sorry i meant if x < y.
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
Apr 13th, 2006
0

Re: modulus operator %

Quote originally posted by degamer106 ...
i'm sorry i meant if x < y.
Yes, then result will be x.
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Aug 29th, 2010
0
Re: modulus operator %
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Waqaskhan411 is offline Offline
1 posts
since Aug 2010
Aug 29th, 2010
0
Re: modulus operator %
I think you've misunderstood how modulus works, or maybe I've misunderstood you

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:
  1. int mod(int a, int b)
  2. {
  3. for(; a > b; a -= b);
  4.  
  5. return a;
  6. }

So if x = 1, y = 8

if 1 > 8 we take away 8, but it isn't so...
answer is 1
Reputation Points: 12
Solved Threads: 7
Junior Poster in Training
Auraomega is offline Offline
57 posts
since Jul 2010
Dec 7th, 2010
0
Re: modulus operator %
Then tell me what would be 3*3*3%4?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Daita is offline Offline
14 posts
since Dec 2010
Dec 7th, 2010
0
Re: modulus operator %
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..
Last edited by Shankye; Dec 7th, 2010 at 5:21 am.
Reputation Points: 48
Solved Threads: 16
Junior Poster
Shankye is offline Offline
168 posts
since Nov 2010
Dec 7th, 2010
0
Re: modulus operator %
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
Last edited by ankitcuul; Dec 7th, 2010 at 1:53 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ankitcuul is offline Offline
2 posts
since Dec 2010
Dec 7th, 2010
0
Re: modulus operator %
Check this
precedence of all operators ..

http://www.difranco.net/cop2220/op-prec.htm
Reputation Points: 48
Solved Threads: 16
Junior Poster
Shankye is offline Offline
168 posts
since Nov 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Passing parameters problem
Next Thread in C Forum Timeline: Downloading Header Files





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC