C Program For Atm Cash Withdrawal Transaction
ATMs are ubiquitous, providing convenient access to funds. Simulating an ATM cash withdrawal transaction in C offers a practical way to understand fundamental programming concepts like input handling, conditional logic, and basic security checks. In this article, you will learn how to build a C program to simulate an ATM cash withdrawal.
Problem Statement
The core problem in simulating an ATM cash withdrawal is to accurately reflect the transaction process. This includes verifying credentials, checking account balance, enforcing withdrawal limits, and dispensing the correct amount of cash while updating the balance. This requires careful handling of user input, conditional logic, and simple data storage.
Example
Here's what a simple ATM withdrawal interaction might look like:
Welcome to the ATM!
Enter your account balance: 1000
Enter the amount you wish to withdraw: 300
Withdrawal successful!
Remaining balance: 700
Background & Knowledge Prerequisites
To follow along with this tutorial, readers should have a basic understanding of:
- C programming language fundamentals (variables, data types).
- Input/Output operations (
printf,scanf). - Conditional statements (
if-else). - Basic arithmetic operations.
Use Cases or Case Studies
Here are practical examples highlighting different ATM withdrawal scenarios:
- Standard Cash Withdrawal: A user requests a specific amount, and if funds are available, the transaction proceeds, and the balance is updated.
- Insufficient Funds: A user attempts to withdraw more than their current balance, leading to a transaction denial and an appropriate error message.
- Invalid Amount: A user enters a non-positive or non-numeric amount (though this simulation focuses on positive checks), prompting the system to request a valid entry.
- Daily Withdrawal Limit: The ATM enforces a maximum daily withdrawal amount, preventing a user from exceeding this limit even if they have sufficient funds.
- PIN Validation: Before any transaction, the user must correctly enter a Personal Identification Number (PIN) to gain access, adding a layer of security.
Solution Approaches
Approach 1: Basic Cash Withdrawal Simulation
This approach demonstrates a straightforward cash withdrawal, checking only for a sufficient account balance.
// Simple ATM Cash Withdrawal Simulation
#include <stdio.h>
int main() {
float balance = 1000.00; // Initial account balance
float withdrawAmount; // Amount to withdraw
printf("Welcome to the ATM!\\n");
printf("Your current balance is: %.2f\\n", balance);
// Step 1: Get withdrawal amount from user
printf("Enter the amount you wish to withdraw: ");
scanf("%f", &withdrawAmount);
// Step 2: Check for valid withdrawal amount
if (withdrawAmount <= 0) {
printf("Invalid withdrawal amount. Please enter a positive value.\\n");
}
// Step 3: Check for sufficient balance
else if (withdrawAmount > balance) {
printf("Insufficient funds. Your current balance is %.2f.\\n", balance);
}
// Step 4: Process withdrawal if valid
else {
balance -= withdrawAmount; // Deduct amount from balance
printf("Withdrawal successful! Please take your cash.\\n");
printf("Your remaining balance is: %.2f\\n", balance);
}
printf("Thank you for using the ATM.\\n");
return 0;
}
Sample Output:
Welcome to the ATM!
Your current balance is: 1000.00
Enter the amount you wish to withdraw: 300
Withdrawal successful! Please take your cash.
Your remaining balance is: 700.00
Thank you for using the ATM.
Sample Output (Insufficient Funds):
Welcome to the ATM!
Your current balance is: 1000.00
Enter the amount you wish to withdraw: 1200
Insufficient funds. Your current balance is 1000.00.
Thank you for using the ATM.
Stepwise Explanation:
- Initialize a
balancevariable with a predefined amount (e.g.,1000.00). - Prompt the user to enter the desired
withdrawAmountusingscanf. - Validate the
withdrawAmount:
- If it's zero or negative, display an error for an invalid amount.
- If it's greater than the
balance, display an error for insufficient funds.
- If the amount is valid and sufficient funds are available, deduct the
withdrawAmountfrom thebalance. - Confirm the successful withdrawal and display the new
balance.
Approach 2: ATM Withdrawal with PIN Validation and Daily Limit
This enhanced approach adds PIN validation and enforces a maximum daily withdrawal limit for a more realistic simulation.
// ATM Withdrawal with PIN and Daily Limit
#include <stdio.h>
int main() {
int correctPin = 1234; // Predefined correct PIN
int enteredPin; // User entered PIN
float balance = 1500.00; // Initial account balance
float dailyWithdrawalLimit = 500.00; // Maximum allowed withdrawal per day
float withdrawnToday = 0.00; // Amount already withdrawn today
float withdrawAmount; // Amount to withdraw
printf("Welcome to the ATM!\\n");
// Step 1: PIN Validation
printf("Please enter your 4-digit PIN: ");
scanf("%d", &enteredPin);
if (enteredPin != correctPin) {
printf("Incorrect PIN. Transaction cancelled.\\n");
return 0; // Exit program if PIN is wrong
}
printf("PIN accepted.\\n");
printf("Your current balance is: %.2f\\n", balance);
printf("Your daily withdrawal limit is: %.2f\\n", dailyWithdrawalLimit);
printf("You have already withdrawn: %.2f today.\\n", withdrawnToday);
// Step 2: Get withdrawal amount
printf("Enter the amount you wish to withdraw: ");
scanf("%f", &withdrawAmount);
// Step 3: Validate withdrawal amount
if (withdrawAmount <= 0) {
printf("Invalid withdrawal amount. Please enter a positive value.\\n");
}
// Step 4: Check daily withdrawal limit
else if ((withdrawnToday + withdrawAmount) > dailyWithdrawalLimit) {
printf("Daily withdrawal limit exceeded. You can withdraw up to %.2f more today.\\n", dailyWithdrawalLimit - withdrawnToday);
}
// Step 5: Check for sufficient balance
else if (withdrawAmount > balance) {
printf("Insufficient funds. Your current balance is %.2f.\\n", balance);
}
// Step 6: Process withdrawal if all checks pass
else {
balance -= withdrawAmount;
withdrawnToday += withdrawAmount;
printf("Withdrawal successful! Please take your cash.\\n");
printf("Your remaining balance is: %.2f\\n", balance);
printf("You have now withdrawn %.2f today.\\n", withdrawnToday);
}
printf("Thank you for using the ATM.\\n");
return 0;
}
Sample Output:
Welcome to the ATM!
Please enter your 4-digit PIN: 1234
PIN accepted.
Your current balance is: 1500.00
Your daily withdrawal limit is: 500.00
You have already withdrawn: 0.00 today.
Enter the amount you wish to withdraw: 400
Withdrawal successful! Please take your cash.
Your remaining balance is: 1100.00
You have now withdrawn 400.00 today.
Thank you for using the ATM.
Sample Output (Incorrect PIN):
Welcome to the ATM!
Please enter your 4-digit PIN: 5678
Incorrect PIN. Transaction cancelled.
Sample Output (Exceeding Daily Limit):
Welcome to the ATM!
Please enter your 4-digit PIN: 1234
PIN accepted.
Your current balance is: 1500.00
Your daily withdrawal limit is: 500.00
You have already withdrawn: 0.00 today.
Enter the amount you wish to withdraw: 600
Daily withdrawal limit exceeded. You can withdraw up to 500.00 more today.
Thank you for using the ATM.
Stepwise Explanation:
- Define a
correctPin, aninitial balance, adailyWithdrawalLimit, andwithdrawnTodayvariables. - Prompt the user to enter their
enteredPin. - Compare
enteredPinwithcorrectPin. If they don't match, display an error and exit the program. - If the PIN is correct, display account information and withdrawal limits.
- Prompt the user for the
withdrawAmount. - Validate the
withdrawAmountfor positive values. - Check if the
withdrawAmountwould exceed thedailyWithdrawalLimitwhen added towithdrawnToday. If so, display a warning about the remaining limit. - Check for
insufficient fundsagainst the currentbalance. - If all conditions are met, deduct the
withdrawAmountfrombalanceand add it towithdrawnToday. - Confirm the successful withdrawal and display the updated
balanceand the totalwithdrawnTodayamount.
Conclusion
Simulating an ATM cash withdrawal in C demonstrates fundamental programming constructs like conditional logic, user input, and variable management. Starting with a basic balance check and extending to PIN validation and daily limits showcases how to build increasingly robust and realistic applications. These principles form the basis for more complex transaction systems.
Summary
- ATM withdrawal simulations use C to process user input and manage financial data.
- Basic simulations involve checking account balance before dispensing cash.
- Advanced simulations incorporate security features like PIN validation.
- Transaction limits, such as daily withdrawal limits, add realism and practical constraints.
- Conditional statements (
if-else) are crucial for handling different transaction outcomes and error conditions.