Thread: Who can figure this one out?

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99

    Who can figure this one out?

    Write a function that takes an integer value and returns the number with its digits reversed. For example, given the number 7631, the function should return 1367.

    I have no idea how to even start after i put the basic stuff in like the #include and int main. Somebody want to show me how they would do it and based off your program I'll do a similiar one.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    read about integer operators / and % and think what will give you

    number /10 and number %10
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by jturner38 View Post
    Write a function that takes an integer value and returns the number with its digits reversed. For example, given the number 7631, the function should return 1367.

    I have no idea how to even start after i put the basic stuff in like the #include and int main. Somebody want to show me how they would do it and based off your program I'll do a similiar one.
    Say your integer was 21. The modulus operator is very nice for this, because it throws out the normal divide by 10 (which is what you want since we're base 10), answer, and shows you the remainder that is left over. 21 mod 10 == 1.

    Now we need to make the number 1 into the char '1'. The C library's itoa() is one way to do that.

    So the one's column digit (1) has been handled. The 21 is still there, but we don't want to handle the 1 again, so we get rid of it, by dividing by 10. Now the number 2 is left in the one's column. Again, itoa() can be used on it, and the result added to the char we took out before (the one).

    If you did this in a loop, it would be easy to put each digit into a char array[], and then print the array in the order you want.

    This is a simple while or for loop block of code. You'll find the result very satisfying.

  4. #4
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    The problem can be solved by arithmetic means only:

    Code:
    int rev(int x)
    {
            int ret = 0;
    
            while(x) {
                    ret *= 10;
                    ret += x % 10;
    
                    x /= 10;
            }
    
            return ret;
    }
    Of course, this will need slight adjustments for negative values. If you stick with Adak's approach, here's a tip for choosing the right array size in advance: ceil(log(x, b)) will compute the number of digits needed to represent x in base b.

    Greets,
    Philip

    PS: this solution is not intended to be used for copy'n'paste. On the other hand, you don't need to invent the trick with % and / yourself. From my experience, understanding it one time makes you able to remember it forever.
    Last edited by Snafuist; 03-05-2009 at 04:15 AM.
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You could also convert it to a string and then reverse the string, but that would probably be too wasteful.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    You could also convert it to a string and then reverse the string, but that would probably be too wasteful.
    Yes, seeing as converting it to a string involves the same number of essentially the same math operations, it seems a bit "going round the block to get next door".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Cool a complete answer

    Code:
    int main() // starting of main funtion
    {
        
         int number, reverse=0, temporarydata;
    
    clrscr(); //to clear the screen
    
    printf("enter the number");      //printing and scanning
    scanf("%d", &number);              
    
      while(number!=0)
    {
           
        temporarydata= number%10;  //  for last digit
      reverse=  reverse*10 + temporarydata //allgori. for storring reverse 
                                                                    //trie it yourself
    
      number=number \10;  decreement by 10
    }
    
    printf("The reverse number is : %d", reverse);
    
    getch();
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3 dimensional figure volume
    By thekautz in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2009, 05:22 PM
  2. can't figure out what's with this code
    By silverstein101 in forum C++ Programming
    Replies: 8
    Last Post: 04-16-2008, 12:45 AM
  3. trying to figure out someone's code for a plugin
    By paulpars in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:57 AM
  4. I can't figure this out!
    By paulntysmom in forum C Programming
    Replies: 8
    Last Post: 04-07-2006, 03:52 AM
  5. newb to C, cant figure something out.
    By Phate4219 in forum C Programming
    Replies: 16
    Last Post: 03-06-2006, 01:47 AM