- #1

OrbitalPower

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:

12

123

1234

or 1, 11, 111, 1111, and so on.

- C/++/#
- Thread starter OrbitalPower
- Start date

- #1

OrbitalPower

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:

12

123

1234

or 1, 11, 111, 1111, and so on.

- #2

OrbitalPower

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++

- #3

- 283

- 0

Code:

```
int num=1;
for (int i=2; i<len; ++i)
{
num*=10;
num+=i;
}
```

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.

- #4

- 283

- 0

Code:

```
#include <sstream>
int str_to_int(std::string str)
{
int num=0;
std::istringstream is(str);
is>>num;
return num;
}
std::string int_to_str(int num)
{
std::ostringstream os;
os<<num;
return os.str();
}
```

- #5

Strilanc

Science Advisor

- 600

- 216

To append at the low end, multiply by 10 and add [digit].

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:

12

123

1234

or 1, 11, 111, 1111, and so on.

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).

- #6

OrbitalPower

Code:

```
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.

- #7

- 30,863

- 7,468

- #8

- 1

- 0

Code:

```
maxappends = 8 # actually its max appends -2
num = 1
for i in range(2,maxappends):
num = num*10 + 1
```

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

Code:

```
maxappends = 20; num = 0
for i in range(maxappends):
num = num + ((i % 9) + 1) * 10**(i)
print i,num
```

- #9

OrbitalPower

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.In Python, the above would become:

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

*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].

- Last Post

- Replies
- 5

- Views
- 571

- Last Post

- Replies
- 9

- Views
- 17K

- Last Post

- Replies
- 9

- Views
- 4K

- Last Post

- Replies
- 20

- Views
- 32K

- Last Post

- Replies
- 9

- Views
- 1K

- Last Post

- Replies
- 3

- Views
- 2K

- Last Post

- Replies
- 26

- Views
- 8K

- Replies
- 8

- Views
- 1K

- Replies
- 3

- Views
- 517

- Last Post

- Replies
- 9

- Views
- 4K