Answer:
input price
set change to hundred minus price
set quarter to change divided by 25
set change to change mod 25
set dime to change divided by 10
set change to change mod 10
set nickel to change divided by 5
set change to change mod 5
set penny to change
print "You bought an item for price cents and gave me a dollar. Your change is: "
if quarter is equal to 1
print quarter
else if quarter is greater than 1
print quarter with "quarters"
if dime is equal to 1
print dime
else if dime is greater than 1
print dime with "dimes"
if nickel is equal to 1
print nickel
else if nickel is greater than 1
print nickel with "nickels"
if penny is equal to 1
print penny
else if penny is greater than 1
print penny with "pennies"
Step-by-step explanation:
One more understandable way of writing the above code is to use different names for change as:
input price
set change to hundred minus price
set quarter to change divided by 25
set quarter_remainder to change mod 25
set dime to quarter_remainder divided by 10
set dimes_remainder to quarter_remainder mod 10
set nickel to dimes_remainder divided by 5
set nickel_remainder to dimes_remainder mod 5
set penny to nickel_remainder
print "You bought an item for" price"cents and gave me a dollar. Your change is: "
if quarter is equal to 1
print quarter "quarter"
else if quarter is greater than 1
print quarter "quarters"
if dime is equal to 1
print dime "dime"
else if dime is greater than 1
print dime "dimes"
if nickel is equal to 1
print nickel "nickel"
else if nickel is greater than 1
print nickel "nickels"
if penny is equal to 1
print penny "penny"
else if penny is greater than 1
print penny "pennies"
I will explain the pseudocode with help of an example:
Suppose price = 75
Then change = 100 - price = 100 - 75 = 25
change = 25
quarter = change/25 = 25/25 = 1
quarter = 1
quarter_remainder = change % 25 = 25%25 = 0
dime = 0/10 = 0
Now all other values are 0.
Now the following line is printed on screen:
You bought an item for 75 cents and gave me a dollar. Your change is:
Now program moves to if part
if quarter == 1
This is true because value of quarter is 1
print quarter "quarter"
Now the following line is printed on screen:
1 quarter
So the complete output of the pseudocode is:
You bought an item for 75 cents and gave me a dollar. Your change is:
1 quarter