programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Bear Bibeault
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Tim Cooke
• Liutauras Vilda
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• fred rosenberger
• salvin francis
Bartenders:
• Piet Souris
• Frits Walraven
• Carey Brown

# Separating Digits with Modulus

Greenhorn
Posts: 9
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!

Sheriff
Posts: 7647
522
• 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?

Greenhorn
Posts: 9
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: 7647
522

Adam Christopher Martin wrote:That's actually a very helpful breakdown

Did you understand? Or still have some doubts about it?

Greenhorn
Posts: 9
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: 7647
522
Then welcome to the Ranch

Greenhorn
Posts: 9
Thankyou, and what a fine ranch it is too!

Liutauras Vilda
Sheriff
Posts: 7647
522
• 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

Greenhorn
Posts: 9
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: 7647
522
• 1

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.

 No more fooling around. Read this tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton