Thread: Printing integers backwards

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    8

    Printing integers backwards

    Could someone give me some help on creating a function that takes an integer value from function main and passes it to a function that reverses the integer value and passes it back to function main to be printed.

    ie: 1234 would be sent back 4321.

    Thank you!

    basia

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You could identify each digit apart by realising that

    1234 = 4 + 3 * 10 + 2 * 100 + 1 * 1000

    Each identified digit can be stored backwards in an array. So the first digit is placed in the last place in the array.

  3. #3
    uninteresting
    Join Date
    Jun 2002
    Posts
    66
    You can get each of the digits by dividing the number by ten. Take the remainder and that's the last digit of the number. The quotient is the rest of the digits. So make the quotient your 'new number', then loop back until your quotient is zero. For example:

    num is initially 1234
    Here's what happens:

    digit = num % 10 //num == 1234, digit == 4
    myarray[1] = digit
    num = num / 10 //num now equals 123

    digit = num % 10 //num == 123, digit == 3
    myarray[2] = digit
    num = num / 10 //num is now 12

    digit = num % 10 //num == 12, digit == 2
    myarray[3] = digit
    num = num / 10 //num is now 1

    digit = num % 10 //num == 1, digit == 1
    myarray[4] = digit
    num = num / 10 //num is now 0

    //num is 0, so we're done. myarray now equals 4321.
    *** TITANIC has quit (Excess Flood)

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    char *_itoa( int value, char *string, int radix );

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define NUM_DIGITS
    
    char ch[NUM_DIGITS + 1]; 
    int i = 10;
    
    atoi(i,ch,10);
    
    printf("%d as a string %s\n",i,ch);
    reverse ch, and print it again.
    Last edited by no-one; 06-28-2002 at 11:57 AM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    8
    Thank you Shiro but I don't quite understand.

    The function I need to create does not know what integer the user is going to enter. It could be anywhere from 1-n number of digits.

    I need to declare a function prototype and function definition that takes the integer and returns the integer backwards without using an array. The question comes from a chapter in my book that is just dealing with passing between functions. Arrays are not covered until the next chapter.

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    The algorithm Tman gave, was what I meant. To convert the array to an integer, you could almost do the same. But instead of dividing, you should do multiplying with 10, so like this:

    array [0] * 10^0 +
    array [1] * 10^1 +
    array [2] * 10^2 +
    ... +
    array [n] * 10^n

    Assume array has the following values:

    array [0] = 1;
    array [1] = 2;
    array [2] = 3;
    array [3] = 4;
    array [4] = 5;

    Then the calculation would be done as:

    integer = 1 * 1 + 2 * 10 + 3 * 100 + 4 * 1000 + 5 * 10000 = 12345

    [edit]
    In programming this would be done as a loop, it could look like:

    Code:
    integer := 0;
    for i from 0 to N do
        integer = integer + array [i] * 10^i;
    [/edit]
    Last edited by Shiro; 06-28-2002 at 12:11 PM.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Why make it so complicated with arrays when you can easily reverse an integer into another integer:
    Code:
    static unsigned reverse ( unsigned val )
    {
      unsigned ret = 0;
      do
        ret = ( val % 10 ) + ret * 10;
      while ( ( val /= 10 ) != 0 );
      return ret;
    }
    By the way, if this is homework then you'd best understand how it works if you plan on turning it in. Teachers have a way of asking you how you came up with something if it looks like it was..."borrowed".

    -Prelude
    My best code is written with the delete key.

  8. #8
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >Why make it so complicated with arrays

    It is easier to understand, because it gives a bit more insight in the working of decimal numbers.

    But you could also convert the integer to a string, reverse the string and convert the string back to integer.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could do it another way that none of you've shown...

    Code:
    sprintf( buf, "%d", mynumber );
    for( x = strlen(buf-1); x > -1; x-- )
        printf("%c",buf[x]);
    After all, the task is just to print the number reversed...

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Printing integers in hex
    By skapykat in forum C Programming
    Replies: 5
    Last Post: 10-06-2008, 02:05 PM
  4. Replies: 2
    Last Post: 02-11-2008, 03:49 AM
  5. Replies: 6
    Last Post: 08-04-2003, 10:57 AM