Like Tree1Likes

Help ! to evalute 1 % 10 ?

This is a discussion on Help ! to evalute 1 % 10 ? within the C Programming forums, part of the General Programming Boards category; 1234 % 10 = 4 (4 is the reminder when 1234 is divided by 10) 156 % 10 = 6 ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Help ! to evalute 1 % 10 ?

    1234 % 10 = 4 (4 is the reminder when 1234 is divided by 10)
    156 % 10 = 6
    12 % 10 = 2

    i could understand how the value of above expression is evaluated but
    i couldn't figure out how to determine the value of these expression

    9 % 10 = ?
    1 % 10 = ?

    please help me to evaluate the value of the expression when dividend is less than divisor

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Divide normally, save the remainder as your answer. Incidentally this is true for all modulo problems.
    smokeyangel likes this.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    ^^ what whiteflags said. This is just modulo arithmetic -- which is well defined. 9 doesn't divide by 10 even once so the remainder is 9. -1 is also correct, but C and C++ will give you the positive answer if you pass in 2 positive numbers.

  4. #4
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    9 % 10 = 9
    1 % 10 = 1

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  5. #5
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    u can broaden ur concept by knowing that

    -9 % 10 = -9
    -1 % 10 = -1
    -11 % 10 = -1

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Quote Originally Posted by smokeyangel View Post
    -1 is also correct...
    divident = divisor*quotent + remainer <==>
    remainer = divident - divisor*quotent

    divident = 9
    divisor = 10
    quotent = 0

    remainer = 9 - 10*0 = 9


    I'm curious to see your explanation why you think -1 is also correct.
    Devoted my life to programming...

  7. #7
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by GReaper View Post
    divident = divisor*quotent + remainer <==>
    remainer = divident - divisor*quotent

    divident = 9
    divisor = 10
    quotent = 0

    remainer = 9 - 10*0 = 9


    I'm curious to see your explanation why you think -1 is also correct.
    cuz if u add 1 to 9 u get 10 (which is divisible by 10....10%10=0)
    so, in mathematics we use two conventions...
    9%10 = 9
    also
    9%10= -1

    but C deals with only one aspect..(only the positive one...it doesn't cares about pure mathematics )

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Quote Originally Posted by rajarshi View Post
    9 % 10= 9
    also
    9 % 10= -1
    Show me one practical application of that (second) convention.
    I've never seen (9% 10 =-1) anywhere .

    but C deals with only one aspect..
    C deals with what you make it deal with.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by manasij7479 View Post
    Show me one practical application of that (second) convention.
    I've never seen (9% 10 =-1) anywhere .


    C deals with what you make it deal with.

    there is a convention...to can pick up any higher mathematics number theory book and u can find it...

    well the practical application is like this...
    think of a clock..the clocks' entire clock surface is divided in 12 parts....suppose u have ur dinner at 10....and now the clock points at 9....so for the clock to point at 10...either it has to so 9 units backward or 1 unit forward....in both cases it points at 10 ...
    and now u can have ur dinner !!

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Quote Originally Posted by rajarshi View Post
    think of a clock..the clocks' entire clock surface is divided in 12 parts....suppose u have ur dinner at 10....and now the clock points at 9....so for the clock to point at 10...either it has to so 9 units backward or 1 unit forward....in both cases it points at 10 ...
    That somewhat makes sense.
    But the clock is circular....but the number line isn't.
    So.. couldn't calculations potentially be off by the numerical value of the divisor when an ambiguity exists?
    Last edited by manasij7479; 11-26-2011 at 10:28 PM.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by manasij7479 View Post
    That somewhat makes sense.
    But the clock is circular....but the number line isn't.
    So.. couldn't calculations potentially be off by the numerical value of the divisor when an ambiguity exists?
    number line is not circular...
    but the mod operator when implemented on the number line performs the particular action on a circular path...
    like while using a mod operator..
    u can get answers as
    0 1 2 3 4 5 6 7 8 9 and again 0 (similar to the clock analogy)

    don't consider the calculations only one way...
    here goes the calculations

    9%10=9 (convention 1)
    OR
    9%10= -1 (convention 2)

    so u can write

    0*10 + 9 = 9 (convention 1)
    OR
    0*10 + (10-1) = 9 (convention 2)

    convention 2 , I am using (10-1) as we know that we are performing the calculations on a %10
    u get get to know what was the divisor by looking at the (0*10) part !!

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  12. #12
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    That's bulls**t! Here we're talking about absolute numbers, not void mathematical theories. If you take the formula I gave and use it correctly you'll see that what you say is impossible.
    Devoted my life to programming...

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    Quote Originally Posted by GReaper
    Here we're talking about absolute numbers, not void mathematical theories. If you take the formula I gave and use it correctly you'll see that what you say is impossible.
    smokeyangel and rajarshi are correct to say that 9 is congruent to -1 modulo 10. The confusion is that % is often called the modulo operator, but it would be more accurate to call it the remainder operator. That said, given smokeyangel's caveat about the context of C and C++, I do not find smokeyangel to be wrong. rajarshi's use of the clock analogy is a common way to explain modular arithmetic, which is sometimes called clock arithmetic.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by GReaper View Post
    That's bulls**t! Here we're talking about absolute numbers, not void mathematical theories. If you take the formula I gave and use it correctly you'll see that what you say is impossible.
    This isn't bulls**t...this is pure number theory in mathematics !!

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Quote Originally Posted by rajarshi View Post
    u can broaden ur concept by knowing that

    -9 % 10 = -9
    -1 % 10 = -1
    -11 % 10 = -1
    If you mean C, the answer has the sign of the divisor not the dividend.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21