0% found this document useful (0 votes)
4 views11 pages

ATM Withdrawal Algorithm in Code

The document teaches how to implement an ATM algorithm in three languages: Portugol, Javascript, and Java. The algorithm accepts a withdrawal amount and returns the minimum number of bills of R$100, R$50, R$20, R$10, R$5, R$2, and R$1 needed to complete the withdrawal.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

ATM Withdrawal Algorithm in Code

The document teaches how to implement an ATM algorithm in three languages: Portugol, Javascript, and Java. The algorithm accepts a withdrawal amount and returns the minimum number of bills of R$100, R$50, R$20, R$10, R$5, R$2, and R$1 needed to complete the withdrawal.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ATM algorithm in Portugol, Javascript, and Java.

Hello Readers,

In today's post, I will teach you how to create an ATM algorithm.

Basically, the statement asks:

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 seems to be a greedy type algorithm, see the definition:

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

Enter an integer number:

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

while (WithdrawnAmount >= 100)

AmountWithdrawn = AmountWithdrawn - 100

Note100Counter++

while (WithdrawnAmount >= 50)

ValorSacado = ValorSacado–50

Note50Counter++

while (AmountWithdrawn >= 20)

ValorSacado = ValorSacado–20

Nota20Counter++

while (WithdrawnAmount >= 10)

WithdrawnAmount = WithdrawnAmount - 10

Nota10Contador++

while (WithdrawnAmount >= 5)

ValorSacado = ValorSacado–5
Note5Counter++

while (WithdrawnAmount >= 2)

WithdrawnAmount = WithdrawnAmount - 2

Note2Counter++

while (WithdrawnAmount == 1)

WithdrawnAmount = WithdrawnAmount - 1

Note1Counter++

write("Notes of 100 withdrawals: ", Nota100Contador, "\n")

Write("50 withdrawals notes: ", Nota50Contador, "\n")

write("Notes of 20 withdrawals: ", Nota20Contador, "\n")

write("10 draws notes: ", Nota10Contador, "\n")

write("Notes of 5 pulls: ", Nota5Contador, "\n")

Notes from 2 shots:

write("Notes from 1 pulls: ", Nota1Contador, "\n")

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 = prompt(“Digite o valor que deseja sacar:”);

valorSaque = parseInt(valorSaque);

if (withdrawalValue < 1) {

alert("Invalid value");

} else {

while (withdrawalValue >= 100) {

withdrawalAmount = withdrawalAmount - 100;

Nota100Count++;

while (withdrawalValue >= 50) {

valorSaque = valorSaque–50;

Nota50Count++;

while (withdrawalValue >= 20) {

withdrawalAmount = withdrawalAmount - 20;

Nota20Count++;

while (valorSaque >= 10) {

valorSaque = valorSaque–10;

Nota10Count++;

while (withdrawalAmount >= 5) {


withdrawalAmount = withdrawalAmount - 5;

Nota5Count++;

while (withdrawalAmount >= 2) {

withdrawalAmount = withdrawalAmount - 2;

Nota2Count++;

while (withdrawalAmount == 1) {

valorSaque = valorSaque–1;

Nota1Count++;

alert("R$100 notes withdrawn: " + Nota100Count);

alert("R$50 notes withdrawn: " + Nota50Count);

alert("R$20 notes withdrawn: " + Nota20Count);

alert("R$10 notes withdrawn: " + Nota10Count);

alert("R$5 notes withdrawn: " + Nota5Count);

alert("R$2 notes withdrawn: " + Nota2Count);

alert('R$1 notes withdrawn: ' + Nota1Count);

alert("Total of notes:" + (Nota100Count + Nota50Count + Nota20Count + Nota10Count + Nota5Count +


Note2Count + Note1Count

</script>

</body>

</html>
Java

import [Link];

public class Withdrawal {

public static void main(String[] args) {

Scanner s = new Scanner([Link]);

String str = [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;

int WithdrawnAmount = [Link](str);

while (WithdrawnAmount >= 100)

{
WithdrawnAmount = WithdrawnAmount - 100;

Nota100Counter++;

while (WithdrawnAmount >= 50)

WithdrawnAmount = WithdrawnAmount – 50;

Note50Counter++;

while (WithdrawnValue >= 20)

ValorSacado = ValorSacado–20;

Note20Counter++;

while (WithdrawnAmount >= 10)

WithdrawnAmount = WithdrawnAmount - 10;

Note10Counter++;

while (WithdrawnAmount >= 5)

WithdrawnAmount = WithdrawnAmount - 5;

Note5Counter++;

while (AmountWithdrawn >= 2)

{
WithdrawnAmount = WithdrawnAmount - 2;

Note2Counter++;

while (WithdrawnValue == 1)

WithdrawnAmount = WithdrawnAmount - 1;

Note1Counter++;

[Link]("100 notes withdrawn: " + Nota100Contador + ".");

[Link]("50 notes withdrawn: " + Nota50Contador + ".");

[Link]("20 notes withdrawn: " + Nota20Contador + ".");

[Link]("10 notes withdrawn: " + Nota10Contador + ".");

[Link]("Notes of 5 withdrawn: " + Nota5Contador + ".");

[Link]("2 draws notes: " + Nota2Contador + ".");

[Link]("1 pulled notes: " + Nota1Contador + ".");

Total = Nota100Counter +

Note50Counter+

Note20Counter+

Nota10Contador+

Note5Counter+

Note2Counter+

Note1Counter;

[Link]("Total Grades: " + Total);

}
}

You might also like