Thread: How to display single digit int as four digit C++ Programming

  1. #1
    Registered User
    Join Date
    Mar 2012
    Location
    Coimbatore,INDIA
    Posts
    1

    Post How to display single digit int as four digit C++ Programming

    Hi all,

    How to display single digit int as four digit in C++.For example, i want to display the digit 12 as 0012 and 8 as 0008.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Set the width to 4 and the fill character to '0'. This can be easily done by #include <iomanip> with:
    Code:
    std::cout << std::setw(4) << std::setfill('0') << num << std::endl;
    By the way, we don't normally consider 12 to be a digit, unless you are expressing the number in base 13 or more.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-17-2010, 11:31 AM
  2. Replies: 13
    Last Post: 11-13-2009, 08:47 AM
  3. Verifying single digit input
    By Syked4 in forum C Programming
    Replies: 8
    Last Post: 05-31-2005, 07:11 PM
  4. leading zero on single digit int
    By confuted in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2003, 04:48 PM
  5. ANSI value of a single digit
    By Iamien in forum C++ Programming
    Replies: 6
    Last Post: 05-24-2003, 05:56 PM