Hello,
I am asking the user for an integer and then I need to print out the number that the user entered backwards. I am having a hard time doing this! I know how this achieved, you just take the "x%10"(mod) of the number and print after each time until the number reaches zero.

for example: user enters: 1557
1557%10= 7 ->print
155%10= 5 -> print
15%10= 5 ->print
1%10=1 ->print for the last time b/c after getting 1 then next mod is zero.

My program is incorrect and I know what I am doing wrong, but I dont know how to create the right code, can someone please help?

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

int main() {

int num, i;
printf("Enter a positive integer.\n");
scanf("%d", &num);

while (num > 0) {

num=(num%10);

printf("%d", num);
num--;

}

system("PAUSE");
return 0;

}```

2. Replace num-- by num /= 10;

3. You also need to get rid of this line:

Code:
`num=(num%10);`
Since you are reducing num to the last digit, it will be the only digit that will be printed before it reaches zero.

Do the modulus directly in the print or use a temporary variable. What you are doing in the above line is assigning the result of num%10 to num, so if you enter 1234, after the first iteration num will be 4. Then as claudiu pointed out, you will need to make your number ten times smaller to get to the next digit.

Code:
`printf("%d", num%10);`

4. Originally Posted by claudiu
Replace num-- by num /= 10;
What you said is useless, I appreciate the effort though.
num /= 10 does not do anything it does not find the remainder. Maybe you meant num%=10 but that is the same as what I have in my code--> num= num %10.

Any other ideas? The comment directly above me isn't working either.

Thanks though, trying stuff out and adjusting my code helps my understanding even if it does not work correctly at first.

5. You will need to divide num by ten. With the adjustment you code would look something like this:

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

int main()
{
int num;
printf("Enter a positive integer.\n");
scanf("%d", &num);

while (num > 0) {
printf("%d", num%10);
num /= 10;
}

return 0;
}```

6. Originally Posted by matthayzon89
What you said is useless, I appreciate the effort though.
num /= 10 does not do anything it does not find the remainder. Maybe you meant num%=10 but that is the same as what I have in my code--> num= num %10.

Any other ideas? The comment directly above me isn't working either.

Thanks though, trying stuff out and adjusting my code helps my understanding even if it does not work correctly at first.
How the !@%@#\$% do you think you're going to get from 1557 to 155 except by dividing by 10? HINT: % is not division. DOUBLE HINT: You don't want num = num % 10.

7. Compose a string from a number, reverse the string, then make the string a number again. sscanf and sprintf will be helpful functions. You have to avoid moving the '-' character but that's all there is to it, OP.

8. Originally Posted by tabstop
How the !@%@#\$% do you think you're going to get from 1557 to 155 except by dividing by 10?
By subtracting 1402?

Quzah.

9. Originally Posted by matthayzon89
What you said is useless, I appreciate the effort though.
num /= 10 does not do anything it does not find the remainder. Maybe you meant num%=10 but that is the same as what I have in my code--> num= num %10.

Any other ideas? The comment directly above me isn't working either.

Thanks though, trying stuff out and adjusting my code helps my understanding even if it does not work correctly at first.
Lol, I'm sorry i'm being useless right now, but you do really need to divide by 10 if you hope to print every digit of your number. Right now your while is probably going to iterate forever.

Get this: say num = 123.

Code:
```while(num > 0){
num = num % 10; /* now num = 123 %10 = 3; */
}```
Second run:

Code:
```while(3 > 0){
num = 3 % 10; /* num is still 3 */
}```

And so on forever.

10. LOLOLOL!!!

Haha

Thanks for the advice guys! You right. While working on this problem I noticed that THAT dividing by 10 is the only way but I was trying to either divide by 10 OR use mod%10, I didn't think of combining them which was my mistake.

Thanks for the help

11. And here I was gong to suggest a simple hand held mirror! Always too late!!