Question:
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.
Solution:
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.
Solution:
put int at line 10
ReplyDelete16th & 17th lines are incorrect I think soo...
ReplyDelete:P
ReplyDeleteIN JAVA:
ReplyDeleteclass Tester {
public static void main(String[] args) {
int noone = 9;
int nofive =3*5;
int nofivereqd = 0;
int amtonereqd = 0;
int total = nofive+noone;
int amt = 21;
if (amt>total) {
System.out.println("-1");
}
else if (amt <= total) {
nofivereqd = amt/5;
System.out.println("The number of 5 notes required "+ nofivereqd );
amtonereqd = amt%5;
System.out.println("number of one required " + amtonereqd);
}
else {
System.out.println("-1");
}
}
}
Thank you bro..
ReplyDeleteIf I have 10 1Rs note 1 5rs note, and amount to be paid is 12rs, then how what is out put of above program?
ReplyDeleteclass Tester {
ReplyDeletepublic static void main(String[] args) {
// Implement your code here
int purchaseAmount = 19;
int $1Notes = 3;
int $5Notes = 3;
int $1NotesNeeded = 0;
int $5NotesNeeded = 0;
$5NotesNeeded = purchaseAmount/5;
$1NotesNeeded = purchaseAmount%5;
if ($5NotesNeeded<=$5Notes && $1NotesNeeded<=$1Notes)
{
System.out.println("$5 notes needed = "+$5NotesNeeded);
System.out.println("$1 notes needed = "+$1NotesNeeded);
}
else if ($5NotesNeeded>$5Notes && $1NotesNeeded<$1Notes)
{
$1NotesNeeded = purchaseAmount-$5Notes*5;
$5NotesNeeded = $5Notes;
System.out.println("$5 notes needed = "+$5NotesNeeded);
System.out.println("$1 notes needed = "+$1NotesNeeded);
}
else
System.out.println(-1);
}
}