Thread: Need help with reversing number in C

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    51

    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. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    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
    - An additional variable
    - 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. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by thebenman View Post
    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. #4
    Registered User
    Join Date
    Sep 2014
    Location
    SE Washington State
    Posts
    65
    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. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    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. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reversing a Number
    By sunny` in forum C Programming
    Replies: 6
    Last Post: 03-02-2012, 09:09 AM
  2. Reversing digits in C
    By Digital Thought in forum C Programming
    Replies: 22
    Last Post: 10-15-2010, 06:34 AM
  3. A Recursive function for Reversing number?
    By Deshe in forum C++ Programming
    Replies: 4
    Last Post: 11-27-2005, 07:12 PM
  4. help with reversing something in a linklist
    By Axel in forum C Programming
    Replies: 43
    Last Post: 10-23-2005, 07:14 AM
  5. Reversing a sentence.
    By Roaring_Tiger in forum C Programming
    Replies: 13
    Last Post: 08-30-2004, 01:40 AM