Thread: help with displaying a number backward

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    help with displaying a number backward

    how do i display a number backward?
    i know i need to use % 10. but if i use this i will only be able to get the tenth digit.

    ex 17 % 10 = 7. how do i get the 1 from the first digit? thanks

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    If you're allowed to, you could convert the number to a std::string, then reverse that using reverse() from <algorithm>.

    http://www.parashift.com/c++-faq-lit....html#faq-39.1
    http://www.cppreference.com/cppalgorithm/reverse.html

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    i can't use string. my professor said so.

    he mention that i need to extract each digits separately.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    divide by ten to right shift by one decimal number: 152/10=15 15/10=1.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    1. Take a number from the user
    2. While "number" is more than 0...
      • Output "number" % 10
      • Divide "number" by 10
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  4. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 10:38 AM
  5. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM