C PROGRAMMING PROGRAM • SWITCH & MENU PROGRAMS

Build a Simple Banking Menu Using switch

Learn how to build a simple banking menu using switch using a clear C program.

IntermediateswitchBalance updateValidation

PROBLEM UNDERSTANDING

Input and expected output

Sample input
1
250
Sample output
Balance = 1250.00

COMPLETE C PROGRAM

Complete C implementation

switch-bank-menu.c
Open in compiler
#include <stdio.h>

int main(void)
{
    int choice;
    double balance = 1000.0, amount;
    printf("1. Deposit  2. Withdraw  3. Balance\nChoose: ");
    scanf("%d", &choice);
    switch (choice) {
        case 1:
            printf("Enter amount: "); scanf("%lf", &amount);
            if (amount > 0) balance += amount;
            else { puts("Invalid amount."); return 0; }
            break;
        case 2:
            printf("Enter amount: "); scanf("%lf", &amount);
            if (amount > 0 && amount <= balance) balance -= amount;
            else { puts("Invalid amount or insufficient balance."); return 0; }
            break;
        case 3: break;
        default: puts("Invalid choice."); return 0;
    }
    printf("Balance = %.2f\n", balance);
    return 0;
}

GUIDED CODE TOUR • NOT LIVE EXECUTION

Study the program line by line

Use the real compiler button above to run and debug with different inputs.

CURRENT STEP

Select Start to walk through the important lines.

SELECTED LINE

No line selected

EXPECTED OUTPUT FOR THE SAMPLE

Balance = 1250.00
0%Step 0 of 0

PROGRAM EXPLANATION

Algorithm and explanation

  1. Read the required input values.
  2. Select one banking operation, validate its amount and update the balance safely.
  3. Display the computed result.

Select one banking operation, validate its amount and update the balance safely.

EFFICIENCY

Time and space complexity

Time complexity

O(1)

Auxiliary space

O(1)

DEBUGGING CHECKLIST

Common mistakes

Check this

Use the correct format specifier for every variable.

Check this

Initialize variables before using their values.

Check this

Check braces, semicolons and input order carefully.

Try it yourself

Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.