Algorithm for ATM Operations
Imperative Algorithm:
1. Start
2. Initialize balance = 1000.0
3. Loop until the user selects Exit:
1. Display the menu:
2. Welcome to the ATM
1. Check Balance
2. Deposit Money
3. Withdraw Money
4. Exit
3. Prompt user for input: choice ← read_input()
4. Perform actions based on choice:
If choice = 1 (Check Balance):
Print "Your balance is: balance"
If choice = 2 (Deposit Money):
Prompt "Enter deposit amount:"
Read amount from input
Convert amount to a number
Update balance ← balance + amount
Print "Your new balance is: balance"
If choice = 3 (Withdraw Money):
Prompt "Enter withdrawal amount:"
Read amount from input
Convert amount to a number
If amount > balance, print "Insufficient balance!", go back to
menu
Else, update balance ← balance - amount
Print "Your new balance is: balance"
If choice = 4 (Exit):
Print "Thank you for using the ATM!"
Break loop and exit program
Else (Invalid Input):
Print "Invalid option, please try again."
4. End
Declarative Algorithm:
1. Define a function ATM(balance) that:
o
Takes balance as input
o
Displays a menu and waits for user input
o
Calls a helper function based on user choice
o
Recursively calls itself with an updated balance until Exit is chosen
2. Define helper functions for transactions:
o
Check_Balance(balance): Returns the balance.
o
Deposit(balance, amount): Returns balance + amount.
o
Withdraw(balance, amount):
If amount > balance, return "Insufficient balance"
Else, return balance - amount.
3. Map user choices to functions:
o
"1" → Check_Balance(balance)
o
"2" → Ask for deposit amount → Call Deposit(balance, amount)
o
"3" → Ask for withdrawal amount → Call Withdraw(balance, amount)
o
"4" → Print "Thank you for using the ATM!" and exit
o
Invalid input → Show error message and restart
4. Repeat until Exit is chosen.
Example Execution:
Welcome to the ATM!
1. Check Balance
2. Deposit Money
3. Withdraw Money
4. Exit
Choose an option: 2 Enter deposit amount: 500 Your new balance is: ₹1500.0
Choose an option: 3 Enter withdrawal amount: 2000 Insufficient balance!
Choose an option: 3 Enter withdrawal amount: 300 Your new balance is: ₹1200.0
Choose an option: 1 Your balance is: ₹1200.0
Choose an option: 4 Thank you for using the ATM!