posted 3 years ago

The following code splits an integer into separate digits. It then adds 7 to each of those digits, and then finds the remainder when the result is divided by ten. To do this it uses the Modulus operator.

It's a homework task that someone asked for help on, on StackOverflow, and I felt curious so I had a go at writing it myself.

I understand completely about what modulus does vis-à-vis fetching the remainder, but I don't understand why using the modulus operator gets you each digit individually?

for example, this snippet splits the digits of an integer - but how??

and why does number have to be reassigned as number / 10?

Apologies if this is stupid - I just can't wrap my head around it right now!

It's a homework task that someone asked for help on, on StackOverflow, and I felt curious so I had a go at writing it myself.

I understand completely about what modulus does vis-à-vis fetching the remainder, but I don't understand why using the modulus operator gets you each digit individually?

for example, this snippet splits the digits of an integer - but how??

and why does number have to be reassigned as number / 10?

Apologies if this is stupid - I just can't wrap my head around it right now!

Mulder, it's me.

posted 3 years ago

Hello Adam Christopher Martin.

First of all there's not such a stupid questions in this forum. We are all here to learn. So don't worry about it.

Solving problems like this, the best approach is to get pencil and piece of paper - so you could write it down and everything could become clear.

Let's take a number, for instance 123

number = 123

Print (123 % 10) = 3

number = 123 / 10 = 12 (since it is integer you get whole number by dividing it, not a float 12.3)

Print (12 % 10) = 2

number = 12 / 10 = 1

Print (1 % 10) = 1

end, since

Did it become more clear?

- 1

Adam Christopher Martin wrote:I understand completely about what modulus does vis-à-vis fetching the remainder, but I don't understand why using the modulus operator gets you each digit individually?

and why does number have to be reassigned as number / 10?

Apologies if this is stupid - I just can't wrap my head around it right now!

Hello Adam Christopher Martin.

First of all there's not such a stupid questions in this forum. We are all here to learn. So don't worry about it.

Solving problems like this, the best approach is to get pencil and piece of paper - so you could write it down and everything could become clear.

Let's take a number, for instance 123

number = 123

**1st round**

Print (123 % 10) = 3

number = 123 / 10 = 12 (since it is integer you get whole number by dividing it, not a float 12.3)

**2nd round**

Print (12 % 10) = 2

number = 12 / 10 = 1

**3rd round**

Print (1 % 10) = 1

__number = 1 / 10 = 0__

end, since

Did it become more clear?

posted 3 years ago

- 1

Would you like to end up this thread by trying to simulate that flow with:

Just for practice. Sometimes it looks for yourself you got it, but slightly different number can mess up your minds again.

number = 5000

Just write the flow in the same way over here, so we could make sure you fully understood

Just for practice. Sometimes it looks for yourself you got it, but slightly different number can mess up your minds again.

number = 5000

Just write the flow in the same way over here, so we could make sure you fully understood

posted 3 years ago

Great idea!

Okay, so....

Number = 5000

print(5000 % 10) = 0 because 5000 / 10 = 500.(

Number becomes 500 because 5000 / 10 = 500

print(500 % 10) = 0 because 500 / 10 = 50.(

Number becomes 50 because 500 / 10 = 50

print(50 % 10) = 0 0 because 50 / 10 = 5.(

Number becomes 5 because 50 / 10 = 5

print(5 % 10) = 5 because 5 / 10 = 0.(

Number becomes 0 because 5/10 = 0

so result is

Okay, so....

Number = 5000

**1st Round**print(5000 % 10) = 0 because 5000 / 10 = 500.(

**0**)Number becomes 500 because 5000 / 10 = 500

**2nd Round**print(500 % 10) = 0 because 500 / 10 = 50.(

**0**)Number becomes 50 because 500 / 10 = 50

**3rd Round**print(50 % 10) = 0 0 because 50 / 10 = 5.(

**0**)Number becomes 5 because 50 / 10 = 5

**4th Round**print(5 % 10) = 5 because 5 / 10 = 0.(

**5**)Number becomes 0 because 5/10 = 0

so result is

**0005**which we can reverse by putting in a list and popping them one by one... to get**5000**!Mulder, it's me.