You have x no. of 5 rupee coins and y no. of 1 rupee coins. You want to purchase an item for amount z. The shopkeeper wants you to provide exact change. You want to pay using minimum number of coins. How many 5 rupee coins and 1 rupee coins will you use?
Write a python program which takes input of total_money_to_be_paid and no_of_fives_available and no_of_ones_available respectively, and print the output of no_of_fives_needed and no_of_ones_needed, If exact change is not possible then display -1, write the whole logic in a function and call the function using user given values as parameters.
Note: you have to pay using least number in the coins you have provided.
please find the image below to understand sample input and expected output :)
Answers
total_cost,f_available,o_available = int(input('Total:')),int(input('5 coins available')),int(input('1 coins available'))
f_coins_needed = total_cost//5
left = total_cost - (5*f_coins_needed)
o_coins_needed = left // 1
if o_coins_needed <= o_available and f_coins_needed <= f_available:
print('rs.1 coins needed :%d' %(o_coins_needed), 'rs.5 coins needed: %d' %(f_coins_needed))
else:
print('-1')
Answer:
Explanation:
one=input("no. of coins of one rupees")
five=input("no. of notes of five rupees")
val=input("amount to be paid")
x=val/5
y=val%5
while True:
if y<=one:
if x<=five:
print "no of one rupees coins is %d" %y
print "no. of five rupees notes is %d" %x
break
else:
x=x-1
y=y+5
else:
print "-1"
break
Hope it's correct :-)