Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Can you append digits to a variable in c++ or python

  1. Jun 1, 2008 #1
    Say you have a variable x, that is of type int.

    x = 1;

    I just want to create a while or for loop so that each time it runs it adds another digit to it, such as:


    or 1, 11, 111, 1111, and so on.
  2. jcsd
  3. Jun 1, 2008 #2
    I know you can do this:

    char y;
    y = '1';

    char test[2];

    test[0] = y;
    test[1] = y;
    test[2] = '\0';

    But I wouldn't know how to get it back to int in C++
  4. Jun 1, 2008 #3
    Code (Text):

    int num=1;
    for (int i=2; i<len; ++i)
    Obviously though, you'd have to watch out for integer overflow.

    You could also go with the string method, and if you don't need to actually perform operations on the number then it would work fine and you wouldn't have to worry about overflow. I'd use std::string instead of a char array, though.
  5. Jun 1, 2008 #4
    Also, since you might need to convert a string to an integer or visa versa at some point:
    Code (Text):

    #include <sstream>

    int str_to_int(std::string str)
      int num=0;
      std::istringstream is(str);
      return num;

    std::string int_to_str(int num)
      std::ostringstream os;
      return os.str();
    These can be easily modified for use with other numeric types as well (for all the types std::*stringstream objects can handle anyways) You could even write a generic template function if you felt like it.
  6. Jun 1, 2008 #5


    User Avatar
    Science Advisor

    To append at the low end, multiply by 10 and add [digit].
    To append at the high end, add [digit] * 10 ^ floor(log_10(n)).

    You can compute the log relatively quickly using shifts and a constant for 1/log_2(10).
  7. Jun 1, 2008 #6
    Yes, thank you both JaWiB and Strilanc. I guess to do the 1111+ I'd so something like:

    Code (Text):
        int x;
        x = 1;
       int num=1;
    for (int i=2; i<8; i++)
       num = num * 10;
       num = num + 1;

    I guess I was so busy thinking about trying to convert between types I didn't think of the simple arithmetic solution.

    I also like the append at the high end method.
  8. Jun 1, 2008 #7


    Staff: Mentor

    I don't know how big you want your numbers, but if you want them really big you will need to download an arbitrary precision arithmetic library. There are a bunch available for C++, but I don't know about Python.
  9. Jun 2, 2008 #8
    In Python, the above would become:
    Code (Text):

    maxappends = 8   # actually its max appends -2
    num = 1
    for i in range(2,maxappends):
      num = num*10 + 1
    You get the added benefit of arbitrary length integers: maxappends could be 1001 or more.

    To add digits to the LHS (only makes sense if the digits are different, and if you are not prepending a zero):
    Code (Text):

    maxappends = 20; num = 0
    for i in range(maxappends):
      num = num + ((i % 9) + 1) * 10**(i)
      print i,num
    - Have fun, Paddy :smile:
  10. Jun 9, 2008 #9
    This is built in natively? What I was doing was constructing a program that return the remainder in a number composed entirely of ones with 50 or so digits. The idea was to determine the remainder by using the divisor only a few times into the dividend, as it was also composed entirely of ones. Obviously, to do that in C++ I'd perhaps need one of those libraries needed above. So, basically I just tested my method with smaller numbers.

    *Also fixed an error I made, should be:

    char test[3];

    test[0] = y;
    test[1] = y;
    test[2] = '\0';

    and not char test[2].
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Can you append digits to a variable in c++ or python
  1. Python to C++ (Replies: 9)

  2. Python or C++ first? (Replies: 20)

  3. Learning python and C# (Replies: 14)