Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

help on understanding logic

Tags:

c++

How can I logically understand this:

int low_digit = value % 10;
value /= 10;

Where I am lost is when mod divides and print the remainder. What is it dividing by?

What does value /= 10; mean?

like image 617
JonathanKohn Avatar asked Jul 10 '26 13:07

JonathanKohn


2 Answers

/= is the C++ way of saying:

value = value / 10;

Here value % 10 gives you the reminder of dividing it by 10. Imagine value is 19, this will give you 9, the last digit (reminder of dividing it by 10). The last statement, will divide that value by 10 (integer division), and will give you 1 (the tens digit).

like image 58
Diego Sevilla Avatar answered Jul 12 '26 04:07

Diego Sevilla


This is an assignment shorthand and a more efficient way of saying:

value = value / 10;

Just like:

i++;

is:

i = i + 1; 

or:

i +=1;

You could also do:

value %= 10; // same as value = value % 10;
like image 27
Gabe Avatar answered Jul 12 '26 04:07

Gabe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!