# Thread: Reversing digits in C

1. Originally Posted by ssharish2005
Code:
```#include <stdio.h>

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

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

getchar();
return 0;
}```
ssharish
thanks for replying, but the problem is already solved .. and just so you know, we were not allwed to use "while"

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 % 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.

thanks for posting, problem already solved :P cheers. but i got your idea, its a good one

3. It worked.. but they did not tell us about return 0 as well as of yet so i modified it like the following
As MacGyver mentioned, do not use void main(), use int main(). On a newer version of the C Standard, you should be able to get away with int main() without return 0, but I would rather leave the return 0 there anyway, at least for C.

clrscr() and getch() are both non-standard and your program does not need them, as far as I can tell.

they also didnt teach us anything about libraries like include etc so thats why
Is this in school? If not, I suggest that you quit and demand your money back. They are hopeless in teaching C, and probably in writing C as well.

because 1234 shoud be inteegers
No 1234 is an integer, not four integers.

4. Originally Posted by laserlight
As MacGyver mentioned, do not use void main(), use int main(). On a newer version of the C Standard, you should be able to get away with int main() without return 0, but I would rather leave the return 0 there anyway, at least for C.

clrscr() and getch() are both non-standard and your program does not need them, as far as I can tell.

Is this in school? If not, I suggest that you quit and demand your money back. They are hopeless in teaching C, and probably in writing C as well.

No 1234 is an integer, not four integers.

well our teacher said to use void main as it will eliminate many errors etc but ok i'll be using int main as well thanks... and return 0 maybe they will tell us about it later, as they have not as of yet..

yes this is in college.. not in some other institution hehe :P

clrscr will clear the screen if we dont clear the screen then the old date will also flash on the screen while u running the program in the compiler and without getch the program will execute and quit directly and it wont stay on screen..

5. well our teacher said to use void main as it will eliminate many errors etc but ok i'll be using int main as well thanks... and return 0 maybe they will tell us about it later, as they have not as of yet..
You might want to read the FAQ on main().

clrscr will clear the screen if we dont clear the screen then the old date will also flash on the screen while u running the program in the compiler and without getch the program will execute and quit directly and it wont stay on screen..
If you do want to use them, you should include the relevant header. Chances are it is <conio.h>, but this being non-standard, there is no guarantee that this is the case, and these functions may not even be available. You can roughly simulate getch() using the standard function getchar() from <stdio.h>, but there is no portable and standard way of clearing the screen.

Out of curiosity, what compiler (and IDE) are you using?

6. > well our teacher said to use void main as it will eliminate many errors

Really?! Like what?

Just curious. If he hasn't said anything about it then he's lying worse than most people.

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

int main()
{
long int num,i,x;
i=10;
printf("Enter the integer\n");
scanf("%d",&num);
printf("\nEntered number is %d\n",num);
while(num)
{
x = num;
printf("%d", num % i);
num /= i;

}
}```