Search code examples
c++long-integerdigits

separate digits of a long number in c++


I have for example the long number 12345678901 and I want to get separately each digit to use it. I tried really hard but I didn't make it so far? Any ideas?

but I have a problem in all of them when I try those with a number of 11 digits and more (that what I want) my program stops working I'm running my program in visual studio in other case-smaller numbers--is just fine.. any connection with the fact that my number is long?


Solution

  • This will give you digits in variable b.

    long a = 12345678901;
    while(a > 0) {
       long b = a % 10;
       a /= 10;
    }