439,532 views
26 votes
26 votes
Write a pseudocode algorithm that prompts the user to enter a three-digit number and outputs the hundreds, tens and units.

Example: 523 will output 5 hundreds, 2 tens and 3 units. Use what you learned about division.

User Alchemy
by
2.6k points

1 Answer

18 votes
18 votes

Answer:

INPUT "Enter three-digit number" as number

IF number length equals 3 AND is integer:

hundreds = integer of number / 100

tens = inter of (remainder of (number / 100) / 10)

units = remainder of number / 10

PRINT hundreds + " hundreds"

PRINT tens + " tens"

PRINT units + " units"

ELSE:

PRINT "You need to input a three-digit integer"

Step-by-step explanation:

integer of division is usually obtained by casting to integer with int() (or // in python)

remainder of is usually obtained by using the modulo operator (%)

So, if you know how many number do you expect, you starting diving by 1xx with the same number of digits and go down by always using "remainder" of the last result and integer division of the same amount of digits, until you get to units, that you only use the remainder.

User Marcocamejo
by
2.6k points