Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I have to write a code where if I put a 3 digit number in, it will count how many hundreds, tens and ones are in it e.g 327=3 hunders, 2 tens. 7. ones

num = int(input('Give me a 3 digit number! :'))

div1 = 100
div2 = 110
div3 = 36

hundreds = num // div1
tens = num // div2
ones = num // div3

print("In %d there are %d hundred(s) %d ten(s) and %d ones are found" % (num, hundreds, tens, ones))

Output (It should be; "In 187 there are 1 hundred(s) 8 ten(s) and 7 ones are found")

The real results

Give me a 3 digit number! : 187
In 187 there are 1 hundred(s) 1 ten(s) and 5 ones are found

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use the modulus operator:

num = int(input('Give me a 3 digit number! :'))

hundreds = num // 100
tens = num % 100 // 10
ones = num % 10

print("In %d there are %d hundred(s) %d ten(s) and %d ones are found" % (num, hundreds, tens, ones))

Result:

Give me a 3 digit number! :187
In 187 there are 1 hundred(s) 8 ten(s) and 7 ones are found
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading