ATM Withdrawal Algorithm in Code
ATM Withdrawal Algorithm in Code
Hello Readers,
Create a program that asks the user for an integer amount to withdraw, like in an ATM.
return to the user the quantity of banknotes of 100, 50, 20, 10, 5, 2, and 1 that they would receive, giving the
the smallest possible amount of bills.
Before seeing my reasoning, try to think about how to solve this, scribble on a paper with a possible
code.
To solve this logic we need to think in the most basic way possible, imagine that the value that
the person asked to be given it gradually, note by note, starting from the 100 note, but every time
note given, we would count the part to add up the total of notes at the end.
It is the algorithm design technique that tries to solve the problem by making the locally optimal choice.
in each phase with the hope of finding a global optimum.
Portugol
program
function start()
inteiro ValorSacado
read (AmountWithdrawn)
int Nota100Contador = 0
integer Nota50Contador = 0
int Nota20Contador = 0
int Nota10Contador = 0
integer Nota5Contador = 0
int Nota2Contador = 0
integer Nota1Counter = 0
total integer
Note100Counter++
ValorSacado = ValorSacado–50
Note50Counter++
ValorSacado = ValorSacado–20
Nota20Counter++
WithdrawnAmount = WithdrawnAmount - 10
Nota10Contador++
ValorSacado = ValorSacado–5
Note5Counter++
WithdrawnAmount = WithdrawnAmount - 2
Note2Counter++
while (WithdrawnAmount == 1)
WithdrawnAmount = WithdrawnAmount - 1
Note1Counter++
Total = Nota100Counter +
Note50Counter+
Note 20 Counter
Nota10Accountant+
Note5Counter+
Note2Counter+
Note1Accountant
Total of Notes:
Javascript
<html>
<head>
<title>Teste</title>
</head>
<body>
<script>
var valorSaque;
var Nota100Count = 0;
var Nota50Count = 0;
var Nota20Count = 0;
var Nota10Count = 0;
var Nota5Count = 0;
var Nota2Count = 0;
var Nota1Count = 0;
valorSaque = parseInt(valorSaque);
if (withdrawalValue < 1) {
alert("Invalid value");
} else {
Nota100Count++;
valorSaque = valorSaque–50;
Nota50Count++;
Nota20Count++;
valorSaque = valorSaque–10;
Nota10Count++;
Nota5Count++;
withdrawalAmount = withdrawalAmount - 2;
Nota2Count++;
while (withdrawalAmount == 1) {
valorSaque = valorSaque–1;
Nota1Count++;
</script>
</body>
</html>
Java
import [Link];
int Nota100Counter = 0;
int Nota50Counter = 0;
int Note20Counter = 0;
int Nota10Contador = 0;
int Nota5Counter = 0;
int Nota2Contador = 0;
int Note1Counter = 0;
int Total = 0;
{
WithdrawnAmount = WithdrawnAmount - 100;
Nota100Counter++;
Note50Counter++;
ValorSacado = ValorSacado–20;
Note20Counter++;
Note10Counter++;
WithdrawnAmount = WithdrawnAmount - 5;
Note5Counter++;
{
WithdrawnAmount = WithdrawnAmount - 2;
Note2Counter++;
while (WithdrawnValue == 1)
WithdrawnAmount = WithdrawnAmount - 1;
Note1Counter++;
Total = Nota100Counter +
Note50Counter+
Note20Counter+
Nota10Contador+
Note5Counter+
Note2Counter+
Note1Counter;
}
}