Results 1 to 15 of 15

Thread: Reversing a number (palendrome-ish)

  1. #1

    Thread Starter
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319

    Reversing a number (palendrome-ish)

    I'm trying to create a program that can reverse any number entered by the user. For example, lets say the user enters the number 12345. The program would then reverse the number and display 54321. I'm sure there's a function for this but I'm limited to using division and modulus operations (it's for my c++ class). Thanks ahead of time.

  2. #2
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330

    Lightbulb

    Just an idea, but I would try loading the numbers into a array. then reprint the array in reverse order.
    Matt

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    divide by 10, the rest is the last digit, thus
    while(num){
    cout<< num%10;
    num/=10;
    }
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Hyperactive Member Emo's Avatar
    Join Date
    Jul 2000
    Posts
    428
    hehehe been there, asked that

    http://www.vbforums.com/showthread.p...hreadid=227228

    -Emo
    -=VB6 Enterprise Edition=-
    -=VC++6Enterprise Edition=-
    «¤E³m°O²™¤»

  5. #5

    Thread Starter
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319
    Originally posted by kedaman
    divide by 10, the rest is the last digit, thus
    while(num){
    cout<< num%10;
    num/=10;
    }
    I'm a bit mathematically inept (that's an understatement), could you explain what this does? It did work by the way, thanks.

  6. #6
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    We use base 10 numbers.

    If you take a number like 5, 5 mod 10 gives you five
    integer division retunrs whole numbers, so 12345/10 gives you 1234

    Code:
    1 2 3 4 5  <<- input 
    12345 % 10 = 5
    12345/10 = 1234
    1234 % 10 = 4
    1234/10 = 123
    123 % 10 = 3
    123/10 = 12
    12 % 10 = 2
    12/10 = 1
    1 % 10 = 1
    5 4 3 2 1       <<- output

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The modulo operation gives you the remainder of integer division.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319
    Originally posted by jim mcnamara
    We use base 10 numbers.

    If you take a number like 5, 5 mod 10 gives you five
    integer division retunrs whole numbers, so 12345/10 gives you 1234

    Code:
    1 2 3 4 5  <<- input 
    12345 % 10 = 5
    12345/10 = 1234
    1234 % 10 = 4
    1234/10 = 123
    123 % 10 = 3
    123/10 = 12
    12 % 10 = 2
    12/10 = 1
    1 % 10 = 1
    5 4 3 2 1       <<- output
    I figured it out a few minutes after I posted that question. So, each time you divide by ten, you move the decimal place over by one. And each time you use the modulus operation on the number, it returns the last number (which happens to be the remainder). Integer truncation gets rid of all the numbers past the decimal point. If I had spent a little longer thinking about what division and modulus does to number, I would have figured it out by myself. Oh well, thanks for the help guys.

    My question now is, does this

    Code:
    cout << num%10
    just keep adding the result of that operation into the buffer and then display it to the screen when the loop ends?
    Last edited by Comreak; Feb 25th, 2003 at 10:48 PM.

  9. #9
    Hyperactive Member Emo's Avatar
    Join Date
    Jul 2000
    Posts
    428
    I would give you the code that worked for me but that's no fun

    -Emo
    -=VB6 Enterprise Edition=-
    -=VC++6Enterprise Edition=-
    «¤E³m°O²™¤»

  10. #10

    Thread Starter
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319
    Originally posted by Emo
    I would give you the code that worked for me but that's no fun

    -Emo
    No problem, kedaman's code works great; I understand it too. I was expecting something more complex. What did you do to solve the problem(now that I've got it working), Emo?

  11. #11
    Hyperactive Member Emo's Avatar
    Join Date
    Jul 2000
    Posts
    428
    This is the code I used...

    of couse, being a newbie, right now I'm not able to do that all by myself so I got some help on it...

    PHP Code:
    #include <stdio.h>
    #include <string.h>

    int mainvoid )
    {
        
    char num[256];
        
    int i;

        
    printf"Enter a number: " );
        
    scanf"%s", &num );

        
    printf"\nYour number: %s\n"num );

        
    printf"Your number backwards: " );
        for( 
    0<= strlennum ); i++ ){
            
    printf"%c"num[strlennum ) - i] );
        }
        
    printf("\n");

        return 
    0;


    -Emo
    -=VB6 Enterprise Edition=-
    -=VC++6Enterprise Edition=-
    «¤E³m°O²™¤»

  12. #12

    Thread Starter
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319
    Originally posted by Emo
    This is the code I used...

    of couse, being a newbie, right now I'm not able to do that all by myself so I got some help on it...

    PHP Code:
    #include <stdio.h>
    #include <string.h>

    int mainvoid )
    {
        
    char num[256];
        
    int i;

        
    printf"Enter a number: " );
        
    scanf"%s", &num );

        
    printf"\nYour number: %s\n"num );

        
    printf"Your number backwards: " );
        for( 
    0<= strlennum ); i++ ){
            
    printf"%c"num[strlennum ) - i] );
        }
        
    printf("\n");

        return 
    0;


    -Emo
    Interesting... I don't recognize the method you've used to write to the screen (of course, I'm a newbie too so there's a lot I don't recognize in general). Are those the commands used to print to the screen in C?

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It's the same as keda's with two differences: He uses C functions instead of C++ stream objects (printf instead of cout) and he reads in the number as string, not as number, which saves him the need to convert, but which makes the program accept non-numbers as well. If I give it the input "Hello" it will print "olleH".
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    Originally posted by Comreak

    My question now is, does this

    Code:
    cout << num%10
    just keep adding the result of that operation into the buffer and then display it to the screen when the loop ends?
    it doesn't add to any buffer, it outputs it every time the loop runs, as it gets your number, it outputs it instead of adding it to some buffer to output later.

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No, he's right. cout has an output buffer, but you never know when it's flushed. You can call flush or insert a flush or endl modifier to force flushing though.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width