ATM program in C using switch case एक बेहतरीन beginner-level project है जो आपको C language के दो सबसे important concepts — switch case और do-while loop — को practically समझने में मदद करता है। अगर आप C programming सीख रहे हैं और menu-driven programs बनाना चाहते हैं, तो यह guide आपके लिए perfect है।
इस post में आप step-by-step सीखेंगे कि कैसे एक real ATM जैसा system C language में बनाया जाता है — जिसमें PIN verification, balance check, cash deposit, withdrawal, और PIN change जैसे features होते हैं। हर feature को switch case के अलग-अलग cases से handle किया जाता है, जो इसे एक ideal switch case exercise in C with solution बनाता है।
यहाँ आपको complete source code, program output, line-by-line code explanation, common mistakes, और FAQs मिलेंगे। चाहे आप किसी assignment के लिए यह program लिख रहे हों या simply C programming की practice कर रहे हों — इस guide को पढ़ने के बाद आप खुद से कोई भी menu driven program in C बना सकते हैं।
Table of Contents
Program Statement — ATM Program in C क्या करेगा?
इस program में user को सबसे पहले अपना PIN enter करना होगा। सही PIN verify होने के बाद ATM का main menu screen पर आता है। Menu तब तक दिखता रहता है जब तक user Exit option न चुने।
नीचे दिए गए options program में available हैं:
| Option | Feature | Description |
|---|---|---|
| 1 | Balance Check | Current account balance दिखाए |
| 2 | Cash Deposit | Amount balance में add करे |
| 3 | Cash Withdrawal | Balance check करके amount घटाए |
| 4 | PIN Change | Old PIN verify करके नया PIN set करे |
| 0 | Exit | Program बंद करे |
Note: गलत choice enter करने पर “Invalid Choice” message दिखेगा। यह default case से handle होता है।
Complete ATM Program in C Using Switch Case — Source Code
नीचे दिया गया ATM program in C using switch case fully working है। इसे copy करके किसी भी C compiler (GCC, Turbo C, Code::Blocks) में run कर सकते हैं:
#include <stdio.h>
// ATM Program in C Using Switch Case
// Switch Case Exercise in C With Solution
int main()
{
int choice;
float balance = 10000.00;
float amount;
int pin = 1234;
int enteredPin, newPin;
// PIN Verification
printf("\n=== Welcome to ABC Bank ATM ===\n");
printf("Enter your PIN: ");
scanf("%d", &enteredPin);
if (enteredPin != pin)
{
printf("Invalid PIN! Access Denied.\n");
return 0;
}
printf("PIN Verified! Welcome!\n");
// Main menu loop using do-while and switch case
do
{
printf("\n====== ATM MAIN MENU ======\n");
printf("1. Check Balance\n");
printf("2. Deposit Money\n");
printf("3. Withdraw Money\n");
printf("4. Change PIN\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1: // Check balance
printf("\nYour Current Balance: Rs. %.2f\n", balance);
break;
case 2: // Deposit money
printf("\nEnter amount to deposit: Rs. ");
scanf("%f", &amount);
if (amount > 0)
{
balance += amount;
printf("Rs. %.2f deposited.\n", amount);
printf("New Balance: Rs. %.2f\n", balance);
}
else
{
printf("Invalid amount!\n");
}
break;
case 3: // Withdraw money
printf("\nEnter amount to withdraw: Rs. ");
scanf("%f", &amount);
if (amount <= 0)
{
printf("Invalid amount!\n");
}
else if (amount > balance)
{
printf("Insufficient Balance!\n");
}
else
{
balance -= amount;
printf("Rs. %.2f withdrawn.\n", amount);
printf("Remaining Balance: Rs. %.2f\n", balance);
}
break;
case 4: // Change PIN
printf("\nEnter current PIN: ");
scanf("%d", &enteredPin);
if (enteredPin == pin)
{
printf("Enter new PIN: ");
scanf("%d", &newPin);
pin = newPin;
printf("PIN changed successfully!\n");
}
else
{
printf("Incorrect PIN!\n");
}
break;
case 0: // Exit
printf("\nThank you for using ABC Bank ATM!\n");
break;
default:
printf("Invalid choice! Please enter 0-4.\n");
}
} while (choice != 0);
return 0;
}
Program Output — यह Program कैसे Run होगा?
जब आप यह program compile और run करते हैं और PIN 1234 enter करते हैं, तो output कुछ इस तरह आता है:
Code Explanation — Step-by-Step समझें
इस menu driven program in C को तीन logical parts में समझते हैं:
Part 1: Variable Declarations और Initial Setup
float balance = 10000.00; // Starting balance
int pin = 1234; // Default ATM PIN
int entered_pin, new_pin; // User input के लिए
float amount; // Deposit/Withdrawal amountfloat को balance और amount के लिए इसलिए use किया गया है क्योंकि real money में decimal values होती हैं — जैसे Rs. 1500.50। int को PIN के लिए use किया गया है क्योंकि PIN हमेशा whole number होता है।
Part 2: PIN Verification
Program start होते ही user से PIN माँगा जाता है। अगर PIN गलत हो तो return 0; से program तुरंत बंद हो जाता है — यह एक simple लेकिन effective security layer है।
if (entered_pin != pin)
{
printf("Invalid PIN! Access Denied.\n");
return 0; // Program यहीं रुक जाता है
}Part 3: Do-While Loop और Switch Case का Combination
do-while loop को menu के लिए इसलिए choose किया गया है क्योंकि menu कम से कम एक बार तो दिखना ही चाहिए। अगर सीधे while loop use करते तो condition पहले check होती और कुछ edge cases में menu न दिखता।
do
{
// Menu display
// switch(choice) — user की choice handle करे
}
while (choice != 0);while(choice != 0) का मतलब है — जब तक user 0 (Exit) नहीं दबाता, menu बार-बार दिखता रहेगा।
Switch Case — सभी Cases की Detailed Working
ATM program in C using switch case में हर case एक specific ATM operation को handle करता है:
Case 1 — Balance Check
सबसे simple case। बस current balance print कर देता है। %.2f format specifier ensure करता है कि balance हमेशा 2 decimal places के साथ दिखे — जैसे Rs. 10000.00।
Case 2 — Deposit Money
User से deposit amount input लिया जाता है। amount > 0 check जरूरी है — negative या zero amount को reject करना होता है। Validation के बाद balance += amount से नई balance calculate होती है।
Case 3 — Withdrawal
यह सबसे ज़्यादा validation वाला case है — दो conditions check होती हैं:
- amount > balance → Insufficient Balance (overdraft रोकना)
- amount <= 0 → Invalid amount (negative withdrawal रोकना)
दोनों conditions false होने पर ही balance -= amount execute होता है।
Case 4 — PIN Change
Security के लिए पहले current PIN verify होता है। सही PIN के बाद ही नया PIN pin = new_pin से set होता है। यह real ATMs की तरह ही काम करता है।
Case 0 — Exit
Thank you message print होता है और while(choice != 0) condition false होने से loop बंद हो जाता है।
Default Case — Invalid Input
1-4 या 0 के अलावा कोई भी number enter करने पर यह case execute होता है और user को valid choice enter करने का message मिलता है।
Common Mistakes जो Beginners करते हैं — इन्हें Avoid करें
यह program लिखते समय C beginners अक्सर ये गलतियाँ करते हैं:
1. break; भूल जाना — यह सबसे common और dangerous mistake है। बिना break के “fall-through” होता है — मतलब एक case execute होने के बाद control अगले case में चला जाता है।
case 1:
printf("Balance check\n");
// break; नहीं लिखा — case 2 भी execute होगा!
case 2:
printf("Deposit\n");
break;2. Switch में Float use करना — C language में switch case सिर्फ int और char types support करता है। Float values के लिए if-else use करें।
3. Default Case न लिखना — Unexpected input handle नहीं होती। Professional code में default case न होना एक code quality issue है।
4. Case के अंदर Variable Declare करना बिना Braces के — इससे scope-related compilation errors आते हैं। अगर case के अंदर variable declare करना हो तो {} use करें।
Switch Case vs If-Else — कब क्या Use करें?
| Parameter | Switch Case | If-Else |
|---|---|---|
| Use Case | Fixed values (1, 2, 3) | Range conditions (>, <) |
| Readability | ज़्यादा clean | Complex हो सकती है |
| Data Types | int और char only | सभी types |
| Performance | Faster (jump table) | Comparatively slow |
इस ATM program में switch case perfect choice है क्योंकि हम fixed menu options (0, 1, 2, 3, 4) check कर रहे हैं।
इस Program को और बेहतर कैसे बनाएं? — Advanced Enhancements
अगर आप इस ATM program in C using switch case को और upgrade करना चाहते हैं, तो ये features add कर सकते हैं:
- PIN attempt limit — 3 बार गलत PIN enter करने पर card block हो जाए
- Transaction history — array की मदद से last 5 transactions store करें
- Minimum balance check — withdrawal में minimum balance (जैसे Rs. 500) maintain करें
- Daily withdrawal limit — एक दिन में maximum Rs. 25000 ही निकाल सकते हैं
- Multiple accounts — struct का use करके multiple user accounts manage करें
ये enhancements आपको C programming के advanced concepts जैसे arrays, structs, और functions भी सीखने का मौका देते हैं।
Conclusion ( निष्कर्ष )
इस post में हमने ATM program in C using switch case को पूरी तरह से समझा — variables से लेकर PIN verification, do-while loop, switch case की working, और common mistakes तक। यह program न सिर्फ एक switch case exercise in C with solution है, बल्कि यह आपको real-world logic सोचने और implement करने की practice भी देता है। अब आप खुद से कोई भी menu driven program in C बना सकते हैं — चाहे वो library management system हो, student record system हो, या calculator।
अगर यह post आपके लिए helpful रही हो, तो इसे अपने दोस्तों और classmates के साथ share करें जो C programming सीख रहे हैं। Comment में बताएं कि आप इस program में कौन सा नया feature add करना चाहते हैं, या आगे कौन सा C program देखना चाहते हैं — हम उसे जरूर cover करेंगे!
