Reversing digits in C

This is a discussion on Reversing digits in C within the C Programming forums, part of the General Programming Boards category; Hello.. I wanted to know how can you reverse digits in "C" ? I am a Student of computer sciences ...

1. Reversing digits in C

Hello.. I wanted to know how can you reverse digits in "C" ? I am a Student of computer sciences and we are told about the "If" statements and the "Switch" statements and functions etc.. here's a small code

Code:
```void main()
{
printf("Enter 4 digits");
scanf("%d%d%d%d", &a,&b,&c,&d);```
ok after that I want something which will reverse the digits.. for example if a user inputs "2345" it should be reversed to "5432" in the output..

many told me that it's not possible without loops and blah blah but how can they give us a assigment on something which they never taught us? so i'm sure there's a way.. please do not tell me exactly how to do it as i want to do it myself just give me a rough idea/hint .. thanks

- DiGiTal ThouGhT!

2. If there are a fixed number of digits, it is possible by manually printing them in reverse. Alternatively, you could use recursion, but that depends on whether recursion is included in the "blah blah".

3. Code:
`printf("&#37;d%d%d%d", &d,&c,&b,&a);`

4. First of all, main() should be declared as returning an int.

A common way of doing it is to use basic math to get each digit... ie. division and modulus by 10, 100, etc. etc..

Another way of reversing the digits is to not read it as an int, but to read it as a string, and then reverse it by going backwards char by char.

5. Originally Posted by laserlight
If there are a fixed number of digits, it is possible by manually printing them in reverse. Alternatively, you could use recursion, but that depends on whether recursion is included in the "blah blah".
what's recursion?

6. Originally Posted by Wiretron
Code:
`printf("%d%d%d%d", &d,&c,&b,&a);`

Already tried this, it did not work lol ;P

7. what's recursion?
Recursion.

On a more serious note, you could try searching the Web.

Already tried this, it did not work lol ;P
Wiretron made a typo (or four typos) and so the code prints the addresses, not the values. Try:
Code:
`printf("&#37;d%d%d%d", d, c, b, a);`
Note that this is what I mean by "manually printing them in reverse".

8. Originally Posted by MacGyver
First of all, main() should be declared as returning an int.

A common way of doing it is to use basic math to get each digit... ie. division and modulus by 10, 100, etc. etc..

Another way of reversing the digits is to not read it as an int, but to read it as a string, and then reverse it by going backwards char by char.
division and modulus by 10, 100 etc? can you give me an example? I didnt get it..

and as far as strings are concerned im sure they did not teach us about strings etc yet so im sure they wont give us asigment off-topic lol :P

9. and as far as strings are concerned im sure they did not teach us about strings etc yet so im sure they wont give us asigment off-topic lol :P
What have you been taught?

10. Originally Posted by laserlight
Recursion.

On a more serious note, you could try searching the Web.

Wiretron made a typo (or four typos) and so the code prints the addresses, not the values. Try:
Code:
`printf("%d%d%d%d", d, c, b, a);`
Note that this is what I mean by "manually printing them in reverse".

I already tried both with and without address.. I knew that & will show the address and not the value, and I tried both with and without & yesterday before wiretron told me here =P and ok i'll search for it.. thnx

11. Originally Posted by laserlight
What have you been taught?
functions .. "IF" statements.. "Switch" statements .. that's all..

12. I already tried both with and without address.. I knew that & will show the address and not the value, and I tried both with and without & yesterday before wiretron told me here =P
I think I know why: you're reading them in in a way that you did not expect. 1234 is not 4 integers, but one integer.

I suggest you change to use chars instead of ints, e.g.,
Code:
```#include <stdio.h>

int main()
{
char a, b, c, d;
printf("Enter 4 digits: ");
scanf("&#37;c%c%c%c", &a, &b, &c, &d);
printf("In reverse: %c%c%c%c", d, c, b, a);
return 0;
}```

13. Code:
```#include <stdio.h>

int main()
{
int num = 1234;
int i = 10;

while(num)
{
printf("&#37;d", num % i);
num /= i;
i *= 10;
}

getchar();
return 0;
}```
ssharish

14. Originally Posted by laserlight
I think I know why: you're reading them in in a way that you did not expect. 1234 is not 4 integers, but one integer.

I suggest you change to use chars instead of ints, e.g.,
Code:
```#include <stdio.h>

int main()
{
char a, b, c, d;
printf("Enter 4 digits: ");
scanf("%c%c%c%c", &a, &b, &c, &d);
printf("In reverse: %c%c%c%c", d, c, b, a);
return 0;
}```

thanks alot, It worked.. but they did not tell us about return 0 as well as of yet so i modified it like the following

Code:
```void main()
{
char a, b, c, d;
clrscr();
printf("Enter 4 digits: ");
scanf("%c%c%c%c", &a, &b, &c, &d);
printf("In reverse: %c%c%c%c", d, c, b, a);
getch();
}```
they also didnt teach us anything about libraries like include etc so thats why hehe.. anyway the idea was right about char hehe.. but it also works with "int" i chcked it but with int u have to give 1 by 1 .. like enter a digit then press "enter" then again another digit and press enter then it works.. with char it works like 1234 all at once.. so yah this was what i was lookin for, but i wonder why it dont work same way like char wid int? because 1234 shoud be inteegers

anyway thanks alot.. cheers

15. Say you had a number (integer) from the user: 180, 759.

Now we begin to print it backwards, where un = user number:

Code:
```/*this is a code idea - won't run as is */
temp_un = un
if temp_un > 0 && temp_un &#37; 10 != 0 {
print temp_un % 10  - the left over 1's place value
temp_un = temp_un / 10 - integer division, drops any value less than 1 to 0, perfect for this.
}```
The normal way to do this would be to use a loop (perhaps a while loop, until temp_un was == 0).

If you haven't had loops yet, you could just repeat this if statement, as many times as you needed to in order to cover the whole range of possible positive integers.

1234567 can be just a bunch of char's in C, not a number. Just happens to look like one, but it's not a number. 1234567\0,
with an end of string marker ('\0') elevates these bunch of char's into a single string.

Still not a number, however, but strings can be handled very nicely in ways that numbers can not be (and vice-versa). Strings can be handled quite differently than char's, so the distinction is a very important one.

In any programming, it's critical to know what data belongs to which data type!

Page 1 of 2 12 Last