Like Tree2Likes
  • 1 Post By Salem
  • 1 Post By grumpy

add a leading zero to an integer

This is a discussion on add a leading zero to an integer within the C Programming forums, part of the General Programming Boards category; I'm working to make a program which will subtract a pair of numbers until it reaches kaprekars constant. in cases ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    7

    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
    Code:
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    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.
    iMalc likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    7
    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
    Posts
    5,851
    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.
    iMalc likes this.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,159
    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.
    Code:
    //try
    //{
    	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, 10:32 AM

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