Thread: How to modulo to this digit

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    79

    How to modulo to this digit

    If I enter this input (number): 52483
    (let's call the input 'num')

    how can I modulo to get the '5'?

    I know to get the 3, I only need to do num % 10, but how do I modulo to get the '5'?

    also, I need something that will work if the number is longer or shorter than this input (For example if num were '2342' or longer '1290381' how would I modulo to get the number all the way at the left?)

    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    %10 gets you the right most digit
    /10 drops the right most digit

    Do them both in a loop, and you'll get each digit in turn, reading R->L
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    I've tried while(num != 0) and then what you said... but then it prints 0 for the most left digit... do you know what i'm doing wrong?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    while(number > 0) is better, imo.

    Post your code so we can see just what you're doing. You're modulo'ing first by ten, and then dividing by ten, right?

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    Code:
    while(num > 0) {
    		num / 10;
    		num % 10;
    		mostSig == num;
    	}
    it doesn't print anything now... the output just goes blank.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    while(num > 9) { //>10 is a bug!
      n = num % 10;		
      num =num / 10;
      printf("I removed the digit %d   Number is now: %d\n", n, num);		
    }
    
    printf("Most significant digit is: %d \n", num);
    Try that.
    Last edited by Adak; 02-17-2011 at 08:30 AM.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    Thanks, that works. Can you please explain what I was doing wrong and how your way worked?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    First, the num > 9 had to be changed from num > 0, because you weren't peeling off all the digits. You wanted to keep the left most digit.

    When you do an operation like multiply divide, modulo, etc., you have to have a variable to "catch" the result: num / 10, doesn't catch the result of the division. The answer flies right by and into the night.

    You needed to modulo the number FIRST, and then divide it.

    A common and useful abbreviated idiom in C is: num /= 10, which is exactly the same as the more verbose: num = num /10. Works for all the basic arithmetic operations: num += 5, etc.

    You're welcome
    Last edited by Adak; 02-17-2011 at 08:30 AM.

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    Oh, now I understand... Thanks again!

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Very good!

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Correction:
    Code:
    while (num > 9)    /* not 10 */
        num /= 10;
    
    return num;

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Ack! A bug!!

    Yes, thank you, nonoob! Make that change, C-Man, if you haven't already.

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    For the rest of the class...
    Problem is exacerbated with input number whose digits start with 10... The result would yield 10.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Displaying the rightmost digit of a positive number
    By randoms4ever in forum C Programming
    Replies: 6
    Last Post: 10-04-2009, 07:50 AM
  2. error msg instead of the result so far
    By maybabier in forum C Programming
    Replies: 25
    Last Post: 03-20-2009, 02:45 PM
  3. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  4. newbie programmer - needs help bad.
    By hortonheat in forum C Programming
    Replies: 17
    Last Post: 10-20-2004, 05:31 PM
  5. Roman number converter
    By BalanusBob in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2002, 06:29 AM