How to get digits of a number without converting it to an array of strings / char?

How to get that the digits of a number are in C ++, without converting it to strings or arrays of characters?

+31
c ++
Sep 09 '09 at 5:44
source share
15 answers

Next, numbers are printed in ascending order (i.e. units, then tens, etc.):

do { int digit = n % 10; putchar('0' + digit); n /= 10; } while (n > 0); 
+42
Sep 09 '09 at 5:53
source share

What about floor(log(number))+1 ?

With n digits and using base b, you can express any number up to pow(b,n)-1 . Thus, to get the number of digits of x in base b, you can use the inverse exponentiation function: the base-b logarithm. To handle non-integer results, you can use the floor()+1 trick.

PS: This works for integers, not decimal numbers (in this case, you should know what precision the type you are using).

+19
Sep 09 '09 at 8:01
source share

Because cocking everything, not knowing the question.
Here is my futility attempt:

 #include <iostream> template<int D> int getDigit(int val) {return getDigit<D-1>(val/10);} template<> int getDigit<1>(int val) {return val % 10;} int main() { std::cout << getDigit<5>(1234567) << "\n"; } 
+13
Sep 09 '09 at 7:50
source share

I saw a lot of answers, but they all forgot to use the do {...} while() , which is actually a canonical way to solve this problem and handles 0 correctly.

My solution is based on this on Naveen .

 int n = 0; std::cin>>n; std::deque<int> digits; n = abs(n); do { digits.push_front( n % 10); n /= 10; } while (n>0); 
+11
Sep 09 '09 at 10:33
source share

Do you want something like that?

  int n = 0; std::cin>>n; std::deque<int> digits; if(n == 0) { digits.push_front(0); return 0; } n = abs(n); while(n > 0) { digits.push_front( n % 10); n = n /10; } return 0; 
+8
Sep 09 '09 at 5:53
source share

Something like that:

 int* GetDigits(int num, int * array, int len) { for (int i = 0; i < len && num != 0; i++) { array[i] = num % 10; num /= 10; } } 

Mod 10 will give you numbers. Div 10s will increase the number.

+6
Sep 09 '09 at 5:50
source share

The integer version is trivial:

 int fiGetDigit(const int n, const int k) {//Get K-th Digit from a Number (zero-based index) switch(k) { case 0:return n%10; case 1:return n/10%10; case 2:return n/100%10; case 3:return n/1000%10; case 4:return n/10000%10; case 5:return n/100000%10; case 6:return n/1000000%10; case 7:return n/10000000%10; case 8:return n/100000000%10; case 9:return n/1000000000%10; } return 0; } 
+3
Sep 15 '11 at 18:25
source share

simple recursion:

 #include <iostream> // 0-based index pos int getDigit (const long number, int pos) { return (pos == 0) ? number % 10 : getDigit (number/10, --pos); } int main (void) { std::cout << getDigit (1234567, 4) << "\n"; } 
+3
Sep 15 '11 at 10:40
source share

These solutions are recursive or iterative. Could a more direct approach be a little more efficient?

From left to right:

 int getDigit(int from, int index) { return (from / (int)pow(10, floor(log10(from)) - index)) % 10; } 

From right to left:

 int getDigit(int from, int index) { return (from / pow(10, index)) % 10; } 
+3
Jul 6 2018-12-12T00:
source share

First digit (least significant) = num% 10, second digit = gender (num / 10)% 10, 3rd digit = gender (number / 100)% 10. etc.

+1
Sep 09 '09 at 5:50
source share

A simple solution would be to use a 10th day journal. Returns the total number of digits - 1. This can be fixed by converting the number to an integer.

 int(log10(number)) + 1 
+1
Jul 15 '19 at 23:11
source share

Use the mod 10 and div 10 sequence of operations (regardless of C ++ syntax) to assign numbers one at a time to other variables.

In pseudo code

 lsd = number mod 10 number = number div 10 next lsd = number mod 10 number = number div 10 

etc...

painful! ... but not strings or arrays of characters.

0
Sep 09 '09 at 5:47
source share

Not as cool as Martin York's answer, but addressing only an arbitrary problem:

You can print a positive integer greater than zero, but simply with recursion:

 #include <stdio.h> void print(int x) { if (x>0) { print(x/10); putchar(x%10 + '0'); } } 

This will print the last low digit.

0
Aug 31 '10 at 5:02
source share

A few years ago, in response to the above questions, I would write the following code:

 int i2a_old(int n, char *s) { char d,*e=s;//init begin pointer do{*e++='0'+n%10;}while(n/=10);//extract digits *e--=0;//set end of str_number int digits=es;//calc number of digits while(s<e)d=*s,*s++=*e,*e--=d;//reverse digits of the number return digits;//return number of digits } 

I think the printf (...) function does something similar.

Now I will write the following:

 int i2a_new(int n, char *s) { int digits=n<100000?n<100?n<10?1:2:n<1000?3:n<10000?4:5:n<10000000?n<1000000?6:7:n<100000000?8:n<1000000000?9:10; char *e=&s[digits];//init end pointer *e=0;//set end of str_number do{*--e='0'+n%10;}while(n/=10);//extract digits return digits;//return number of digits } 

<strong> Advantages: search table separately; C, C ++, Java, JavaScript, compatible with PHP; get the number of digits, min comparisons: 3 ; get the number of digits, max comparisons: 4 ; fast code; the comparison is very simple and fast: cmp reg, immediate_data → 1 processor clock speed.

0
Sep 15 2018-11-11T00:
source share

Get all the individual digits into something like an array - two options:

 int i2array_BigEndian(int n, char a[11]) {//storing the most significant digit first int digits=//obtain the number of digits with 3 or 4 comparisons n<100000?n<100?n<10?1:2:n<1000?3:n<10000?4:5:n<10000000?n<1000000?6:7:n<100000000?8:n<1000000000?9:10; a+=digits;//init end pointer do{*--a=n%10;}while(n/=10);//extract digits return digits;//return number of digits } int i2array_LittleEndian(int n, char a[11]) {//storing the least significant digit first char *p=&a[0];//init running pointer do{*p++=n%10;}while(n/=10);//extract digits return pa;//return number of digits } 
0
Sep 15 '11 at 19:55
source share



All Articles