Reversing numbers in c without arrays

• 11-15-2012
jp5a9852
Reversing numbers in c without arrays
As the title says I need to know how to reverse numbers in c without using the arrays. Why? That is part of my assignment. My professor told me that there is a simpler way to do it but I don't know what she is talking about, and I've found nothing in my book about reversing until the array section. By reverse I mean take 123 and make a new number, 321. I need this last part for an assignment that I actually posted earlier. The assignment calls for me to input a number and solve by its reverse. Remember no arrays, thanks!
• 11-15-2012
Salem
This counts as one of those things which is so annoyingly simple that you'll kick yourself for not seeing it earlier.
• 11-15-2012
livin
Quote:

Originally Posted by jp5a9852
As the title says I need to know how to reverse numbers in c without using the arrays. Why? That is part of my assignment. My professor told me that there is a simpler way to do it but I don't know what she is talking about, and I've found nothing in my book about reversing until the array section. By reverse I mean take 123 and make a new number, 321. I need this last part for an assignment that I actually posted earlier. The assignment calls for me to input a number and solve by its reverse. Remember no arrays, thanks!

Do you need to save the reversed number or print it to screen?

If it is just printing out the reverse, I would just repetedly do a number % 10 and print it out. If you need to save it, I would do number % 10 and multiply each extracted digit by its weight.

Eg.

123 % 10 = 3
12 % 10 = 2
1 % 10 = 1

reversed number would be

(3 * 100) + (2 * 10) + (1)

u can save this in a variable and print it out.
• 11-15-2012
jp5a9852
Most of the questions I ask are like that unfortunately, I'm not finding this in my book though so I don't know where to start. So I must ask.
• 11-15-2012
whiteflags
That's sign number one that you aren't learning programming sufficiently.
• 11-15-2012
jp5a9852
I don't need to print out the reverse just solve by it. Also I haven't seen the % used like this, what exactly is it doing?
• 11-15-2012
livin
Quote:

Originally Posted by jp5a9852
I don't need to print out the reverse just solve by it. Also I haven't seen the % used like this, what exactly is it doing?

may be u should look up the Arithmetic operators in C section of the book.

C - Overview of Operator Types, Arithmetic, Bitwise, Assignment, Precedence Table

when u do x % y, u get the reamainder of the division.
• 11-15-2012
stahta01
• 11-15-2012
gratiafide
I, too, am fascinated by that use of the modulus operator. At first I was confused, but now I see why it should work on any number. It's because it's dividing by 10 every time. So the remainder will always be the digit you want next. Bravo, I think that is really smart.
• 11-15-2012
Click_here
Quote:

Originally Posted by gratiafide
I, too, am fascinated by that use of the modulus operator. Surely that doesn't work for all numbers though?

What do you mean by that?

The modulus operator returns the remainder of a divide

 Original quote edited -> See below. [/edit]
• 11-15-2012
gratiafide
I had edited my post almost immediately :)
• 11-15-2012
Click_here
Quote:

Originally Posted by gratiafide
I had edited my post almost immediately :)

No worries -> I've made a note in my post for you.
• 11-15-2012
iMalc
Think of an int variable as a stack...
The item on the top of the stack can be obtained from value%10
You can remove an item off the top off the stack by doing value/=10
The stack is empty when the value is zero
You can push an item on by doing value=value*10+digit

Now, simply do the following in pseudocode:
while stack1 is not empty
* pop top item off stack1
* push item onto stack2

(Note that this is the same procedure as reversing a linked-list in-place)
Done!
• 11-16-2012
nonoob
What did we do in the days before the modulo operator? (in another language such as BASIC). We were still able to solve these problems...
X - X / 10 * 10
This scales the number down for the next iteration if you save the intermediate result as well as obtains the remainder - the units digit.