#include <stdio.h>
// Function declarations
void deposit(float *balance, float amount);
void withdraw(float *balance, float amount);
int main()
{
float balance = 0.0;
int choice;
float amount;
while (1)
{
// Display the menu
printf("\n--- Basic Banking System ---\n");
printf("1. Deposit\n");
printf("2. Withdraw\n");
printf("3. Check Balance\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
// Deposit operation
printf("Enter amount to deposit: ");
scanf("%f", &amount);
deposit(&balance, amount);
break;
case 2:
// Withdraw operation
printf("Enter amount to withdraw: ");
scanf("%f", &amount);
withdraw(&balance, amount);
break;
case 3:
// Display the current balance
printf("Current Balance: %.2f\n", balance);
break;
case 4:
// Exit the program
printf("Exiting the program. Thank you!\n");
return 0;
default:
// Invalid choice handling
printf("Invalid choice! Please select a valid option.\n");
}
}
}
// Function to deposit money into the account
void deposit(float *balance, float amount)
{
if (amount > 0)
{
*balance += amount;
printf("Successfully deposited %.2f. New Balance: %.2f\n", amount, *balance);
} else
{
printf("Invalid deposit amount!\n");
}
}
// Function to withdraw money from the account
void withdraw(float *balance, float amount)
{
if (amount > 0 && amount <= *balance)
{
*balance -= amount;
printf("Successfully withdrew %.2f. New Balance: %.2f\n", amount, *balance);
}
else if (amount > *balance)
{
printf("Insufficient balance! Cannot withdraw %.2f\n", amount);
}
else
{
printf("Invalid withdrawal amount!\n");
}
}
Explanation:
- Header Files:
#include <stdio.h>: This library is used for input/output operations, like printing messages and scanning user input.
- Function Declarations:
deposit: This function is responsible for depositing money into the account.withdraw: This function handles the withdrawal of money from the account.
- Main Function:
float balance = 0.0: The account’s initial balance is set to zero.while (1): This creates an infinite loop to continuously display the menu options until the user chooses to exit.switch (choice): Based on the user’s input, the program either deposits money, withdraws money, checks the balance, or exits.
- Menu Options:
- The program displays a simple menu for the user to select options (Deposit, Withdraw, Check Balance, or Exit).
- For deposit and withdraw options, the user is prompted to input an amount, and the respective function is called.
- Deposit Function:
- This function checks if the deposit amount is valid (greater than 0) and then adds the amount to the balance.
- Withdraw Function:
- This function checks if the withdrawal amount is valid (greater than 0) and that the user has sufficient balance. If both conditions are met, the amount is deducted from the balance.
- Balance Check:
- The current balance is printed when the user selects the “Check Balance” option.
Output:
--- Basic Banking System ---
1. Deposit
2. Withdraw
3. Check Balance
4. Exit
Enter your choice: 1
Enter amount to deposit: 500
Successfully deposited 500.00. New Balance: 500.00
--- Basic Banking System ---
1. Deposit
2. Withdraw
3. Check Balance
4. Exit
Enter your choice: 2
Enter amount to withdraw: 200
Successfully withdrew 200.00. New Balance: 300.00
--- Basic Banking System ---
1. Deposit
2. Withdraw
3. Check Balance
4. Exit
Enter your choice: 3
Current Balance: 300.00
--- Basic Banking System ---
1. Deposit
2. Withdraw
3. Check Balance
4. Exit
Enter your choice: 4
Exiting the program. Thank you!
Summary:
- This program provides basic functionality for depositing and withdrawing money in a simple banking system.
- The menu-driven approach allows users to interact with the system easily.
- Error handling is in place to manage invalid inputs and insufficient balance conditions.