flipping number question..

This is a discussion on flipping number question.. within the C Programming forums, part of the General Programming Boards category; what is the math formula(algorithm) for flipping any number like: 47->74 1655 -> 5561 12345->54321 1190 -> 911 ??...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    flipping number question..

    what is the math formula(algorithm) for flipping any number like:
    47->74
    1655 -> 5561

    12345->54321

    1190 -> 911

    ??
    Last edited by transgalactic2; 12-08-2008 at 02:48 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Use a for loop and divide the number by 10 in each iteration till it's zero while accruing the remainder, which is printed out after the loop is finished. Just my 2c and others may have better ways.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by itCbitC View Post
    Use a for loop and divide the number by 10 in each iteration till it's zero while accruing the remainder, which is printed out after the loop is finished. Just my 2c and others may have better ways.
    Wow itCbitC
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    If you want an actual number that you can use, you could use something like this:
    Code:
    newnum = 0
    for each digit in number:
        digit = number % 10
        newnum = newnum * 10 + digit
    If all you want to do is reverse the data, you could do as itCbitC suggested, or, if you have a string representation of the number, you could just print the string in reverse.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    solved it thanks
    Last edited by transgalactic2; 12-09-2008 at 08:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. help with number menu
    By j4k50n in forum C Programming
    Replies: 12
    Last Post: 04-17-2007, 01:33 AM
  5. Question about limiting the number of user inputs in C
    By chobibo in forum C Programming
    Replies: 15
    Last Post: 08-30-2006, 12:37 AM

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