recursivly reverse a number

This is a discussion on recursivly reverse a number within the C Programming forums, part of the General Programming Boards category; how would you reverse a number , using recursion, in C (i.e. 1234 becomes 4321)...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    2

    Question recursivly reverse a number

    how would you reverse a number , using recursion, in C
    (i.e. 1234 becomes 4321)

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    foo ( x / 10 );
    printf( "%d", x % 10 );

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Just a clarification to Salem's post, if you want to print the number in reverse, print num % 10 first, and then recurse.
    printf ( "%d", x % 10 );
    recurse ( x / 10 );

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  2. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  3. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 12:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21