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.
Answer: C program
Explanation:
// Program in C language.
#include <stdio.h>
#include<conio.h>
int main() {
int z, x, y;
int a, b, c, d;
scanf("%d %d %d",&z,&x,&y);
if (z > 5 * x + y) {
printf("Error: cost more than value of coins.\n");
return(-1);
}
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 denomination 5: %d \n", c);
printf ("Number of coins of denomination 1: %d\n", d);
return (0);
getch();
}
#SPJ2