ATM Program in C Using Switch Case – Complete Guide with Source Code

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 बना सकते हैं।

Program Statement — ATM Program in C क्या करेगा?

इस program में user को सबसे पहले अपना PIN enter करना होगा। सही PIN verify होने के बाद ATM का main menu screen पर आता है। Menu तब तक दिखता रहता है जब तक user Exit option न चुने।

नीचे दिए गए options program में available हैं:

OptionFeatureDescription
1Balance CheckCurrent account balance दिखाए
2Cash DepositAmount balance में add करे
3Cash WithdrawalBalance check करके amount घटाए
4PIN ChangeOld PIN verify करके नया PIN set करे
0ExitProgram बंद करे

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 कुछ इस तरह आता है:

▶️ Output Result
=== 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 0. Exit Enter your choice: 1Your Current Balance: Rs. 10000.00====== ATM MAIN MENU ====== 1. Check Balance 2. Deposit Money 3. Withdraw Money 4. Change PIN 0. Exit Enter your choice: 2Enter amount to deposit: Rs. 5000 Rs. 5000.00 deposited. New Balance: Rs. 15000.00====== ATM MAIN MENU ====== 1. Check Balance 2. Deposit Money 3. Withdraw Money 4. Change PIN 0. Exit Enter your choice: 3Enter amount to withdraw: Rs. 2000 Rs. 2000.00 withdrawn. Remaining Balance: Rs. 13000.00====== ATM MAIN MENU ====== 1. Check Balance 2. Deposit Money 3. Withdraw Money 4. Change PIN 0. Exit Enter your choice: 0Thank you for using ABC Bank ATM!

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 amount

float को 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 करें?

ParameterSwitch CaseIf-Else
Use CaseFixed values (1, 2, 3)Range conditions (>, <)
Readabilityज़्यादा cleanComplex हो सकती है
Data Typesint और char onlyसभी types
PerformanceFaster (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 historyarray की मदद से last 5 transactions store करें
  • Minimum balance check — withdrawal में minimum balance (जैसे Rs. 500) maintain करें
  • Daily withdrawal limit — एक दिन में maximum Rs. 25000 ही निकाल सकते हैं
  • Multiple accountsstruct का 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 करेंगे!

Frequently Asked Questions (FAQs)

Q 1: क्या C में switch case के अंदर string use कर सकते हैं?
Ans: नहीं। C language में switch case सिर्फ int और char data types के साथ काम करता है। String comparison के लिए if-else और strcmp() function use करना पड़ता है। C++ में भी यही limitation है — हालाँकि कुछ compilers extensions provide करते हैं, लेकिन standard C में यह supported नहीं है।
Q 2: Do-while loop और while loop में ATM menu के context में क्या फर्क है?
Ans: while loop पहले condition check करता है — अगर शुरू में ही condition false हो तो loop एक बार भी नहीं चलेगा। do-while loop पहले body execute करता है, फिर condition check करता है — इसलिए loop कम से कम एक बार जरूर चलता है। Menu-driven programs के लिए do-while इसीलिए best choice है क्योंकि menu हमेशा एक बार तो दिखना चाहिए।
Q 3: Switch case में break; न लिखें तो क्या होगा?
Ans: इसे fall-through कहते हैं। अगर किसी case में break; नहीं लिखा तो उस case के execute होने के बाद program अगले case में बिना condition check किए चला जाता है। उदाहरण के लिए, अगर case 2 में break न हो और user 2 press करे, तो case 2 और case 3 दोनों execute हो जाएंगे — जो एक serious bug है।
Q 4: Default case लिखना technically जरूरी है क्या?
Ans: Technically नहीं — program बिना default के भी compile और run होगा। लेकिन यह best practice है। Default case के बिना, अगर user 0-4 के अलावा कोई number enter करे (जैसे 99), तो program चुपचाप कुछ नहीं करेगा और menu फिर दिख जाएगा — user को पता ही नहीं चलेगा कि उसने गलत input दिया। Default case इस situation को gracefully handle करता है।
Q 5: क्या इस ATM program को functions में divide करना चाहिए?
Ans: हाँ, absolutely। एक बड़ा program हमेशा functions में divide करना चाहिए — इसे modular programming कहते हैं। उदाहरण के लिए: checkBalance(), depositMoney(), withdrawMoney(), changePin() — अलग-अलग functions बनाएं। इससे code readable, reusable और easy to debug होता है। Beginners के लिए पहले एक ही main() में सब लिखना ठीक है — concept समझ आने के बाद functions का use करें।

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top