This is the question: Consider a currency system in which there are notes of seven denominations, namely, Rs 1, Rs 2, Rs. 5, Rs. 10, Rs. 50, Rs. 100. If a sum of Rs. N is entered through the keyboard, write a program to compute the smallest number of notes that will combine to give Rs. N.
Here is my code. The program seems to be stuck in infinite loop
public class ReachNwithCurrencyNotes {
public static void main(String[] args) {
int n,temp = 0, a = 1, b = 2, c = 5, d = 10, e = 50, f = 100, sum = 0, countA = 0, countB = 0, countC = 0, countD = 0, countE = 0, countF = 0;
Scanner input = new Scanner (System.in);
System.out.println("Please enter N");
n = input.nextInt();
temp = n;
do {
if (temp-f>=100) {
sum = sum + f;
countF++;
temp = temp - f;
}
else if (temp-e>=50) {
sum = sum + e;
countE++;
temp = temp - e;
}
else if (temp-d>=10) {
sum = sum + d;
countD++;
temp = temp - d;
}
else if (temp-c>=5){
sum = sum + c;
countC++;
temp = temp - c;
}
else if (temp-b>=2){
sum = sum + b;
countB++;
temp = temp - b;
}
else if (temp-a>=1){
sum = sum + a;
countA++;
temp = temp - a;
}
} while (sum<n);
System.out.println("N completed with\n" + countA + "\n" + countB + "\n" + countC + "\n" + countD + "\n" + countE + "\n" + countF);
}
}
Aucun commentaire:
Enregistrer un commentaire