• Post Reply Bookmark Topic Watch Topic
  • New Topic

Separating Digits with Modulus  RSS feed

 
Adam Christopher Martin
Greenhorn
Posts: 9
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Adam Christopher Martin
Greenhorn
Posts: 9
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's actually a very helpful breakdown, thankyou very much Liutauras Vilda!
and how nice to come to a forum where I won't be chastised for my poor maths skills!
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Christopher Martin wrote:That's actually a very helpful breakdown


Did you understand? Or still have some doubts about it?
 
Adam Christopher Martin
Greenhorn
Posts: 9
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand now, yes. I think that following the flow of logic was the main issue for me, so breaking it down like that worked wonders! many thanks!
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then welcome to the Ranch
 
Adam Christopher Martin
Greenhorn
Posts: 9
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou, and what a fine ranch it is too!
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Adam Christopher Martin
Greenhorn
Posts: 9
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great idea!

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!





 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Christopher Martin wrote:That's actually a very helpful breakdown


Great job Adam Christopher Martin.

Thank you for helping me understand, that it was actually helpful.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!