|
-
Feb 24th, 2003, 06:53 PM
#1
Thread Starter
Hyperactive Member
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.
-
Feb 24th, 2003, 08:27 PM
#2
Hyperactive Member
Just an idea, but I would try loading the numbers into a array. then reprint the array in reverse order.
Matt 
-
Feb 25th, 2003, 12:14 AM
#3
transcendental analytic
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.
-
Feb 25th, 2003, 12:28 AM
#4
Hyperactive Member
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Feb 25th, 2003, 06:03 AM
#5
Thread Starter
Hyperactive Member
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.
-
Feb 25th, 2003, 07:05 AM
#6
Frenzied Member
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
-
Feb 25th, 2003, 12:35 PM
#7
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.
-
Feb 25th, 2003, 05:51 PM
#8
Thread Starter
Hyperactive Member
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
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.
-
Feb 25th, 2003, 06:04 PM
#9
Hyperactive Member
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²™¤»
-
Feb 25th, 2003, 06:15 PM
#10
Thread Starter
Hyperactive Member
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?
-
Feb 25th, 2003, 07:34 PM
#11
Hyperactive Member
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 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
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Feb 25th, 2003, 10:42 PM
#12
Thread Starter
Hyperactive Member
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 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?
-
Feb 26th, 2003, 06:42 AM
#13
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.
-
Feb 26th, 2003, 07:11 AM
#14
Frenzied Member
Originally posted by Comreak
My question now is, does this
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.
-
Feb 26th, 2003, 12:40 PM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|