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? If exact change is not possible then display -1.
Answers
// A program in c language.
#include <stdio.h>
main()
{
int z, x, y;
scanf("%d %d %d",&z,&x,&y);
if (z > 5 * x + y) {
print("error: cost more than value of coins.\n");
return(-1);
}
int a,b,c,d;
a = z/5; b = z%5;
if (x <= a) c = x;
else c = a;
d = 5 * c + y - z ;
if ( d < 0 ) {
printf ("not possible to pay exactly. \n");
return(-1);
}
printf ("number of coins of denom 5: %d \n", c);
printf ("number of coins of denom 1: %d\n", d);
return (0);
}
// done.
HOPE IT HELPS YOU ☺ !!
Answer: python program
def make_amount(rupees_to_make,no_of_five,no_of_one):
remaining_fives = no_of_five
remaining_ones = no_of_one
five_needed = rupees_to_make // 5 # number of fives needed
one_needed = rupees_to_make - 5*five_needed
if five_needed > remaining_fives: # If we need more fives than we have
one_needed += 5 * (five_needed - remaining_fives) # Then we need more ones
remaining_fives = 0 # and we use all our fives
else:
remaining_fives -= five_needed # we have enough fives so use them
if one_needed > remaining_ones:
print(-1)
return # We don't have enough ones
else:
remaining_ones -= one_needed # remove the used ones
print("No. of Five needed :", no_of_five- remaining_fives)
print("No. of One needed :", no_of_one- remaining_ones)
make_amount(16,1,15)