ATM program in C using switch case beginners के लिए एक perfect switch case exercise in C with solution है। इस post में आप step-by-step सीखेंगे कि C language में switch case और do-while loop की मदद से एक complete menu-driven ATM system कैसे बनाते हैं — Balance Check, Deposit, Withdrawal और PIN Change जैसे features के साथ। साथ में full source code, program output और detailed explanation भी मिलेगी।
Table of Contents
Program Statement
इस C program में user से PIN input लिया जाता है और verify किया जाता है। सही PIN enter करने पर ATM का main menu दिखता है जिसमें निम्न options हैं:
| Option | Feature | Description |
| 1 | Balance Check | Current balance दिखाए |
| 2 | Cash Deposit | Amount balance में जोड़े |
| 3 | Cash Withdrawal | Balance check करके amount घटाए |
| 4 | PIN Change | Old PIN verify करके नया PIN set करे |
| 5 | Exit | Program बंद करे |
Note: यदि user गलत choice enter करे तो “Invalid Choice” दिखेगा। जब तक user Exit (0) न चुने, menu बार-बार दिखता रहेगा।
Program Code
नीचे दिया गया complete ATM program in C using switch case है — copy करें, compiler में paste करें और 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 entered_pin, new_pin;
// PIN Verification
printf("\n=== Welcome to ABC Bank ATM ===\n");
printf("Enter your PIN: ");
scanf("%d", &entered_pin);
if(entered_pin != pin)
{
printf("Invalid PIN! Access Denied.\n");
return 0;
}
printf("PIN Verified! Welcome!\n");
// Main Menu Loop using do-while + 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 (1-5): ");
scanf("%d", &choice);
switch(choice)
{
case 1: // Balance Check
printf("\nYour Current Balance: Rs. %.2f\n", balance);
break;
case 2: // Deposit
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: // Withdrawal
printf("\nEnter amount to withdraw: Rs. ");
scanf("%f", &amount);
if(amount > balance)
printf("Insufficient Balance!\n");
else if(amount <= 0)
printf("Invalid amount!\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", &entered_pin);
if(entered_pin == pin)
{
printf("Enter new PIN: ");
scanf("%d", &new_pin);
pin = new_pin;
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 1-4 or 0.\n");
}
} while(choice != 0);
return 0;
}
Program Output
जब आप यह program run करते हैं और PIN 1234 enter करते हैं तो नीचे जैसा output आता है:
=== Welcome to ABC Bank ATM ===
Enter your PIN: 1234
PIN Verified! Welcome!
====== ATM MAIN MENU ======
1. Check Balance
2. Deposit Money
3. Withdraw Money
4. Change PIN
5. Exit
Enter your choice (1-5): 2
Enter amount to deposit: Rs. 5000
Rs. 5000.00 deposited.
New Balance: Rs. 15000.00
Enter your choice (1-5): 3
Enter amount to withdraw: Rs. 2000
Rs. 2000.00 withdrawn.
Remaining Balance: Rs. 13000.00
Enter your choice (1-5): 5
Thank you for using ABC Bank ATM!
Code Explanation
इस menu driven program in C को तीन main parts में समझते हैं:
PIN Verification
- Program शुरू होते ही user से PIN input लिया जाता है। अगर PIN गलत है तो program तुरंत बंद हो जाता है:
- Variables: — float balance = 10000.00 — starting balance set होती है।
- int pin = 1234 — यह ATM का default PIN है।
- Input: — scanf(“%d”, &entered_pin) — user से PIN input लिया जाता है।
- Validation: — if(entered_pin != pin) — गलत PIN पर program return 0; से बंद होता है।
Do-While Loop और Menu
- Main menu को do-while loop के अंदर रखा गया है। इसका कारण यह है:
- Do-while loop body पहले execute करता है, फिर condition check करता है।
- Menu कम से कम एक बार तो जरूर दिखना चाहिए — इसलिए do-while best choice है।
- while(choice != 5) — जब तक user 5 (Exit) नहीं दबाता, menu बार-बार दिखता रहता है।
- while loop में condition पहले check होती, इसलिए menu एक बार भी न दिखता — यही फर्क है।
Switch Case — सभी Cases की Working
switch(choice) statement user की choice की value check करता है। नीचे हर case की working:
| Case | Operation | Working |
| case 1 | Balance Check | Current balance %.2f format (2 decimal places) में print होता है |
| case 2 | Deposit | amount > 0 check करके balance += amount से balance update होता है |
| case 3 | Withdrawal | amount > balance पर Insufficient Balance, amount <= 0 पर Invalid amount |
| case 4 | PIN Change | Old PIN verify होता है, फिर pin = new_pin से नया PIN set होता है |
| case 5 | Exit | Thank you message print होता है, do-while condition false होती है |
| default | Invalid Input | 1-5 के अलावा कोई भी input आने पर error message print होता है |
IMPORTANT: हर case के बाद break; जरूर लिखें। break; न लिखने पर Fall-Through होता है — यानी current case execute होने के बाद control अगले case में चला जाता है जो एक common bug है।
Common Mistakes in Switch Case (Avoid करें)
इस switch case program in C को लिखते समय beginners अक्सर ये गलतियाँ करते हैं:
| गलती | क्यों Wrong | सही तरीका |
| break; भूल जाना | Fall-through होता है — सभी cases execute हो जाते हैं | हर case के end में break; लगाएं |
| Float use करना switch में | Switch case सिर्फ int और char support करता है | Float के लिए if-else use करें |
| default case न लिखना | Invalid input handle नहीं होती | हमेशा default: लिखें |
| Case में variable declare करना | Scope problem आती है | Curly braces {} use करें case के अंदर |
Frequently Asked Questions (FAQs)
Q1. क्या switch case में string use कर सकते हैं C में?
Ans: नहीं। C language में switch case सिर्फ integer (int) और character (char) types के साथ काम करता है। String के लिए if-else या strcmp() function use करना पड़ता है।
Q2. Switch case और if-else में क्या फर्क है?
Ans: Switch case तब use करना चाहिए जब एक ही variable की multiple fixed values check करनी हों। यह if-else से fast और ज़्यादा readable होता है। नीचे comparison देखें:
| Parameter | Switch Case | if-else |
|---|---|---|
| Use case | Fixed values check (1, 2, 3…) | Range conditions (>, <, >=) |
| Readability | ज़्यादा readable | Complex हो जाता है |
| Data types | int और char only | सभी types support |
| Speed | Faster (jump table) | Comparatively slow |
Q3. Do-while और while loop में क्या अंतर है?
Ans: while loop में condition पहले check होती है — अगर condition false है तो loop एक बार भी नहीं चलता। do-while में body पहले execute होती है, फिर condition check होती है — इसलिए loop कम से कम एक बार जरूर चलता है। Menu-driven programs के लिए do-while loop हमेशा best choice होता है।
Q4. Default case लिखना जरूरी है क्या?
Ans: Technically जरूरी नहीं, लेकिन best practice यही है कि हमेशा default: लिखें ताकि unexpected inputs gracefully handle हो सकें। Professional programs में default case न होना एक code quality issue माना जाता है।
Conclusion (निष्कर्ष)
इस post में हमने एक complete ATM program in C using switch case बनाया जो switch case exercises in C with solution की दुनिया में एक best example है। यह program beginners को menu driven program in C practically समझने में बहुत help करता है।
- इस program से आपने ये important concepts सीखे:
- Switch case सिर्फ int और char के साथ काम करता है।
- हर case में break; लगाना जरूरी है — वरना fall-through होता है।
- Do-while loop के साथ switch case एक perfect menu system बनाता है।
- Default case हमेशा लिखें — unexpected input handle होती है।
- Input validation (amount > 0, amount <= balance) program को robust बनाती है।
Did you find this post helpful?
इसे अपने दोस्तों के साथ share करें और comment में बताएं कि आप और कौनसे C programs देखना चाहते हैं!
