# Thread: Need help with reversing number in C

1. ## Need help with reversing number in C

Hey,

I wanted to write a program to convert a number into a more readable format. It's like, if the input enters the number as 2361263 the output should be like 2,361,263. I went about this problem like extraction the number first and then if the count was equal to multiple of three i'd print ',' instead of the number.

But initially when i wrote the code for extracting a single digit from the number
Code:
```#include<stdio.h>
void main()
{
unsigned long long num=pow(2,50);
int count=0;
while(num!=0)
{
int last_digit;
last_digit=fmod(num,10);
count+=1;
if(count % 3 ==0)
printf(",");
printf("%d",last_digit);
num/=10;
}
}```
I know that I'd lose the number when i finish printing it, but still I only end up printing the entire number in reverse order.

As in if the input is 1234 the output is 4,321 which is not what i want.

One way of overcoming this problem is to store the values in an array and then reading then back from the end. But i wanted to know if there is a better solution than this? To extract the digits from the number in the same order as it is in the number

2. One way would be to reverse the number before running the code you have. You can do this using the following simple things:

- A loop
- The operators: * + / %

Just make sure that the reversed number is not larger than its data type can hold.

Also, "main()" returns an int: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

Code:
```int main(void)
{
// ...

return 0;
}```

3. Originally Posted by thebenman
But i wanted to know if there is a better solution than this? To extract the digits from the number in the same order as it is in the number
If the integer is nonnegative and you don't want to use a temporary array then you could also use integer division and mod for this. Suppose the number x is 12345. Let i=0 refer to the digit in the 1's place (i.e. "5" in this example) and let i=4 refer to the largest digit (i.e. "1" in this example). These places are also called 10^0 and 10^4 respectively (10 raised to a power). An N digit integer has places 10^0, 10^1, ..., 10^N-1. With this in mind you can extract any digit i from x using integer division followed by mod 10. Example code:

Code:
```int digit = x / pow(10,i);
digit %= 10;```
For example if i = 3, then 12345 / 1000 is 12 after integer division. After the mod 10 it is 2, which is the digit you wanted.

To figure out how many digits are in x you can use log10 of x+1 for all x, x > 0. To handle the case that x = 0, the number of digits is 1. If x is negative, simply print a minus sign and then use the same approach to print -x.

4. I'm new to ANSI C but isn't there a function that converts a number to a string? Like the opposite of atoi().
If not, how hard would it be to cast the number to a string and add the commas? After all, you will end up with a string once you add the commas anyway.

5. I'm new to ANSI C but isn't there a function that converts a number to a string? Like the opposite of atoi().
Some implementations support an "itoa()", but this is non-standard. A better approach would be to use "sprintf()".

If not, how hard would it be to cast the number to a string and add the commas? After all, you will end up with a string once you add the commas anyway.
If you wanted to add commas to a string representation of a number, you would need to ensure the string was large enough to handle the extra characters.

6. You could always use the locale feature of printf.
Code:
```#include <stdio.h>
#include <math.h>
#include <locale.h>
int main(int argc, char *argv[])
{
char *p = setlocale(LC_NUMERIC,argv[1]);
if ( p ) {
int num = 123456789;
printf("%s %'d\n", argv[1], num );
}
return 0;
}```

Run with all the available locales
Code:
```\$ locale -a | while read line ; do ./a.out \$line ; done
C 123456789
C.UTF-8 123456789
en_AG 123,456,789
en_AG.utf8 123,456,789
en_AU.utf8 123,456,789
en_BW.utf8 123,456,789
en_CA.utf8 123,456,789
en_DK.utf8 123.456.789
en_GB.utf8 123,456,789```