# Program to reverse a number. small error with it..

• 11-21-2004
comproghelp
Program to reverse a number. small error with it..
hi! heres my program:

#include <stdio.h>
#include <math.h>
#define NEWLINE '\n'

int main(void){
int num;
int reverse(int n);

printf("Enter a positve integer: \n");
scanf("%i \n", &num);

printf("The reverse number is: %i \n", reverse(num));

return 0;
}

int reverse(int n)
{
int rev_num = 0, k = 0,i =2, mult = 1;
int rev_digit, digitcount;
int digits(int r);

digitcount = digits(n);

while((k <= digitcount) && (digitcount != 0))
{
rev_digit = n % 10;

k++;
mult = pow(10, (digitcount - k));

rev_num = rev_num + (rev_digit * mult);
n = n / 10;
}
return rev_num;
}

int digits(int r){
int count = 0;

while(r != 0)
{
r = r/10;
count++;
}

return count;
}

Output:
Enter a postive number:
8002033 /* for example */
2
/* this is where the problem occurs. after i enter 8002033, the program ask for another number, it doesnt matter what i enter, i just need to enter something */

The reverse is 3302008
/* this part seems correct */

So can anyone help me? i dont know why they need me to enter another number after 8002033!! :confused:
• 11-21-2004
comproghelp
sorry heres the tagged code, i missed the top post

Code:

```#include <stdio.h> #include <math.h> #define NEWLINE '\n' int main(void){         int num;         int reverse(int n);         printf("Enter a positive integer: \n");         scanf("%i \n", &num);         printf("The reverse number is: %i \n", reverse(num));         return 0; } int reverse(int n){         int rev_num = 0, k = 0,i =2, mult = 1, rev_digit, digitcount;                 int digits(int r);                   digitcount = digits(n);         while((k <= digitcount) && (digitcount != 0))                 {                     rev_digit = n % 10;             k++;             mult = pow(10, (digitcount - k));             rev_num = rev_num + (rev_digit * mult);                     n = n / 10;                    }                 return rev_num;                    } int digits(int r){         int count = 0;         while(r != 0)                 {               r  = r/10;               count++;                 }         return count; }```
• 11-22-2004
Thantos
Code:

`scanf("%i \n", &num);`
Try
Code:

`scanf("%i", &num);`
• 11-22-2004
comproghelp
YES!!! it worked!!
thank you!!!
you are brilliant!!!
thank you!!!
thank you!!!
thank you!!!
• 11-22-2004
rmps
Assuming that you're using unsigned int number as input and ASCII,

Code:

```void print_reverse_number(const unsigned int number) {         const unsigned int      quotient = number / 10;         const unsigned int      remainder = number % 10;         putchar(remainder + '0');         if (quotient != 0)                 print_reverse_number(quotient); }```
• 11-22-2004
quzah
Code:

```sprintf( buf, "%d", num ); for( x = strlen( buf ) -1; x > -1; x-- )     putchar( buf[x] );```
Quzah.
• 11-22-2004
Thantos
Bah quzah you forgot to check to see if its negative first ;)
• 11-22-2004
quzah
There you go thinking negative again...

Quzah.
• 11-22-2004
Thantos
Always