Results 1 to 6 of 6

Thread: Converting a double digit to single digit

  1. #1

    Thread Starter
    Hyperactive Member deane034's Avatar
    Join Date
    May 2001
    Location
    Sri Lanka
    Posts
    485

    Question Converting a double digit to single digit

    how do you convert a double digit number, to a single digit in C

    say, if you have 34 i want it to convert to,

    3*10 + 4 does anyone know of a ready-made algo (or code)?

    cheers!
    me.life = VB

  2. #2
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Converting a double digit to single digit

    Why do you want to convert it to the algorithm? That ain't exactly a single digit as you stated in your threads title.

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Re: Converting a double digit to single digit

    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Junior Member
    Join Date
    Feb 2007
    Posts
    28

    Re: Converting a double digit to single digit

    Kedaman that link just shows how to double the integer though.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Re: Converting a double digit to single digit

    Quote Originally Posted by vbleet
    Kedaman that link just shows how to double the integer though.
    Well the example shows how to do that, but thats not the point. I guessed he was asking for how to convert a string of digits to an integer.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Converting a double digit to single digit

    Quote Originally Posted by deane034
    how do you convert a double digit number, to a single digit in C

    say, if you have 34 i want it to convert to,

    3*10 + 4 does anyone know of a ready-made algo (or code)?

    cheers!
    You would need to first convert it into a string, then append the multiplication factor to each digit (char) in the string. The simplest way would involve converting each char back into an integer. There's probably some mathematical way of avoiding the second conversion, but I can't think of it right now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width