PDA

Click to See Complete Forum and Search --> : Reversing a number (palendrome-ish)


Comreak
Feb 24th, 2003, 05:53 PM
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.

MPrestonf12
Feb 24th, 2003, 07:27 PM
Just an idea, but I would try loading the numbers into a array. then reprint the array in reverse order.

kedaman
Feb 24th, 2003, 11:14 PM
divide by 10, the rest is the last digit, thus
while(num){
cout<< num%10;
num/=10;
}

Emo
Feb 24th, 2003, 11:28 PM
hehehe been there, asked that ;)

http://www.vbforums.com/showthread.php?s=&threadid=227228

-Emo

Comreak
Feb 25th, 2003, 05:03 AM
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.

jim mcnamara
Feb 25th, 2003, 06:05 AM
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


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

CornedBee
Feb 25th, 2003, 11:35 AM
The modulo operation gives you the remainder of integer division.

Comreak
Feb 25th, 2003, 04:51 PM
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


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


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?

Emo
Feb 25th, 2003, 05:04 PM
I would give you the code that worked for me but that's no fun ;)

-Emo

Comreak
Feb 25th, 2003, 05:15 PM
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?

Emo
Feb 25th, 2003, 06:34 PM
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...


#include <stdio.h>
#include <string.h>

int main( void )
{
char num[256];
int i;

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

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

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

return 0;

}


-Emo

Comreak
Feb 25th, 2003, 09:42 PM
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...


#include <stdio.h>
#include <string.h>

int main( void )
{
char num[256];
int i;

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

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

printf( "Your number backwards: " );
for( i = 0; i <= strlen( num ); i++ ){
printf( "%c", num[strlen( num ) - 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?

CornedBee
Feb 26th, 2003, 05:42 AM
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".

SteveCRM
Feb 26th, 2003, 06:11 AM
Originally posted by Comreak

My question now is, does this


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. :cool:

CornedBee
Feb 26th, 2003, 11:40 AM
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.