Thread: add a leading zero to an integer

  1. #1
    Registered User
    Join Date
    Mar 2013

    add a leading zero to an integer

    I'm working to make a program which will subtract a pair of numbers until it reaches kaprekars constant.

    in cases where the number is <1000, we are supposed to take the number and add a leading zero to it.

    i.e 999 would become 0999.

    I have a feeling this has something to do with the format specifiers, but I'm under the assumption that the leading zeros are not actually part of the number.

    Am I totally barking up the wrong tree or am I close to what I need?

    Here is my code for reference
    remainders = userInput % 1000;inputVariables[0] = userInput / 1000;
    userInput = remainders;
    remainders = userInput % 100;
    inputVariables[1] = userInput / 100;
    userInput = remainders;
    remainders =userInput % 10;
    inputVariables[2] = userInput / 10;
    userInput = remainders;
    inputVariables[3] = remainders;
    numberY = (inputVariables[0]*(1))+(inputVariables[1]*(10))+(inputVariables[2]*(100))+(inputVariables[3]*(1000)); 
    return numberY;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Leading zeros are purely an artefact of how you decide to present the information to the user.

    They have no mathematical significance at all.

    Look at say "%04d" as a printf format for displaying numbers with leading zeros.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2013
    sounds like thats a dead end then. but i'm still clueless as to how I get numbers like 999 to be interpreted by the program as 0999.

  4. #4
    Registered User
    Join Date
    Jun 2005
    You don't.

    You can output a value 999 with a leading zero, using %04d format specifier. You can interpret input data of the form "0999" as the value 999.

    However, the value 999 is represented using some unique (for your compiler) representation corresponding to its type (eg int, float, double). Typically that represents consists of a sequence of bits that are set on and off - changing any bit changes the value. But that representation does not vary with how the value is input or output.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    The easiest way I can see is to convert the values to and from string form for some of the operations.

    For instance, convert the number to a string, then add leading zeros (if necessary), reverse the string, then convert back to a number again.
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. leading zero's..... ( reading int )
    By sh4k3 in forum C Programming
    Replies: 4
    Last Post: 06-12-2007, 09:03 AM
  2. Leading Underscores
    By laserlight in forum C++ Programming
    Replies: 19
    Last Post: 09-04-2005, 02:16 AM
  3. suppressing leading zero
    By penney in forum C Programming
    Replies: 0
    Last Post: 04-08-2004, 08:46 AM
  4. lost the leading 0, help
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2002, 02:05 PM
  5. Leading astray? (01 - 09)
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-21-2002, 11:32 AM