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.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
1 250
Sample output
Balance = 1250.00
COMPLETE C PROGRAM
Complete C implementation
#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;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
Balance = 1250.00
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Select one banking operation, validate its amount and update the balance safely.
- 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.
