207k views
3 votes
Once upon a time there was a country called Plusplusland. The monetary system they used there was simple: the currency name was the "plussar" and their central bank issued five different banknotes of values 50, 20, 10, 5 and 1 plussar.Your task is to write a program for the ATMs of Plusplusland. The program should find the minimal number of banknotes needed to deliver any amount of money to the client.The Treasury Minister has asked you personally to do this. He expects your code to print the values of all the needed banknotes in a row – this is the format accepted by all ATMs in Plusplusland. Your program will require the use of an integer array combined with different loops to process the currency.Test your code using the sample data provided below......Example input125Example output50 50 20 5Example input127Example output50 50 20 5 1 1Example input49Example output20 20 5 1 1 1 1(HINT: Here is an array that maybe helpful for all known banknotes ordered from the most valuable to the leastint banknotes[] = {50, 20, 10, 5, 1} and then use your skill with array related processing combining different kinds of loops)

User Romina
by
5.0k points

1 Answer

7 votes

Answer:

Hi!

The answer coded in Javascript is:

function minimalNumbersOfBanknotes(input) {

var banknotes = [50, 20, 10, 5, 1];

var output = '';

//Each while writes on output variable the banknote and substract the same quantity on input variable.

//If the input is lesser than the value of while banknote, step into next while.

for (var i = 0; i < banknotes.length; i++) {

while(input >= banknotes[i]) {

output = output + ' ' + banknotes[i];

input = input - banknotes[i];

}

}

return output;

}

User Siega
by
6.1k points