Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding C++ code - "Get the number of digits in an int"

Tags:

c++

while-loop

I am having trouble understanding how exactly this code works:

int length = 1;
int x = 234567545;
while (x /= 10)
   length++;

It is supposed to count the number of digits in the int variable. I don't get how the while loop is working. Does the loop just go to zero and stop by default? Also, why is the length starting at 1?

like image 772
VickTree Avatar asked Jan 10 '19 20:01

VickTree


People also ask

How do you find the number of digits in an integer?

Perhaps the easiest way of getting the number of digits in an Integer is by converting it to String, and calling the length() method. This will return the length of the String representation of our number: int length = String. valueOf(number).

How many digits can int have in C?

The unsigned int can be positive and zero but not negative, so it can store values from 0 to 65,535, or more depending on hardware.

How do you check if a number has a certain digit in C?

bool containsDigit(int number, int digit); If number contains digit, then the function should return true . Otherwise, the function should return false .


3 Answers

There are three things that might be suspicious for you if you are a C++ beginner:

First thing might be operator /=, which combines an integral division (i.e. without remainder), with an assignment. So x /= 10 actually is the same as x = x / 10.

Second, each expression in C++ has - after having been evaluated - a value. For an assignment like (x = 0), the result is the value of x after the assignment, i.e. 0 in this case.

Third, a condition like if (x) ... with x being of integral type has in C++ the same meaning as if(x != 0), i.e. it is false if x equals 0, and it is true if x is anything else but 0.

All together: while ( x /= 10 ) means assign x the value of an integral division by 10 and then compare the value to 0. If 0 is reached, the loop ends.

BTW: length starts with 1, because any number, even 0, comprises at least one digit.

like image 103
Stephan Lechner Avatar answered Oct 17 '22 05:10

Stephan Lechner


x /= 10 continuously divides x by 10, which will make it 0 eventually and cause the while loop to terminate due to 0 being interpreted as false (and any other value than 0 as true).

The reason it starts at length = 1 is because there is always at least 1 digit in the number: if x was from 0 to 9 inclusive, then x /= 10 would cause x to become 0 immediately, meaning nothing inside the loop would execute. Therefore, if length started at 0, it would never get to be incremented to 1 which would be wrong if x was a single digit large.

Manually calculating this example by hand:

  1. 234567545 / 10 = 23456754, which is true, so the while loop continues and length becomes 2.

  2. 23456754 / 10 = 2345675, true. length becomes 3.

  3. 2345675 / 10 = 234567, true. length becomes 4.

  4. 234567 / 10 = 23456, true. length becomes 5.

  5. 23456 / 10 = 2345, true. length becomes 6.

  6. 2345 / 10 = 234, true. length becomes 7.

  7. 234 / 10 = 23, true. length becomes 8.

  8. 23 / 10 = 2, true. length becomes 9.

  9. 2 / 10 = 0, false. The while loop stops with length equal 9.

like image 27
brothir Avatar answered Oct 17 '22 05:10

brothir


The loop

while (x /= 10) {
  length++;
}

will go until the result of x /= 10 evaluates to false, since 0 means false it will go until x /= 10 is 0. Integer division truncates, ensuring the condition will be reached. This can be illustrated by adding a simple debug statement, i.e.

while (x /= 10) {
  length++;
  std::cout << length << " " << x << std::endl;
}

Which outputs

2 23456754
3 2345675
4 234567
5 23456
6 2345
7 234
8 23
9 2
like image 8
William Miller Avatar answered Oct 17 '22 06:10

William Miller