Introduction
In this tutorial, we will create a simple C program that simulates a basic bank account. The program will allow users to perform essential banking functions like depositing money, withdrawing money, and checking their account balance. The objective is to demonstrate basic C programming concepts such as functions, conditionals, and loops to manage the operations of a bank account.
Objective
The goal of this program is to simulate a basic bank account with the following features:
- Deposit: Add funds to the account.
- Withdrawal: Remove funds from the account, ensuring there are sufficient funds available.
- Balance Check: Display the current balance of the account.
By the end of this tutorial, you will have a fully functional banking simulator that can handle these basic operations.
Bank Account Simulator Code
#include float balance = 0.0; // Variable to store account balance // Function to deposit money into the account void deposit(float amount) { if (amount > 0) { balance += amount; printf("Deposited: %.2f\n", amount); } else { printf("Invalid deposit amount.\n"); } } // Function to withdraw money from the account void withdraw(float amount) { if (amount > 0 && amount <= balance) { balance -= amount; printf("Withdrawn: %.2f\n", amount); } else if (amount > balance) { printf("Insufficient funds.\n"); } else { printf("Invalid withdrawal amount.\n"); } } // Function to check the current balance void check_balance() { printf("Current balance: %.2f\n", balance); } // Main function to execute the program int main() { int choice; float amount; while (1) { // Menu for user input printf("\nBank Account Simulator\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: printf("Enter deposit amount: "); scanf("%f", &amount); deposit(amount); break; case 2: printf("Enter withdrawal amount: "); scanf("%f", &amount); withdraw(amount); break; case 3: check_balance(); break; case 4: printf("Thank you for using the Bank Account Simulator!\n"); return 0; default: printf("Invalid choice. Please try again.\n"); } } return 0; }
Explanation of the Program
This program simulates a basic bank account system and consists of the following parts:
- Global Variable: The variable
balance
stores the current balance of the account, initialized to 0.0. - Functions:
deposit(float amount)
: This function allows the user to deposit a specified amount into the account. It checks if the amount is positive before adding it to the balance.withdraw(float amount)
: This function allows the user to withdraw a specified amount from the account. It ensures that there are sufficient funds before completing the withdrawal.check_balance()
: This function displays the current account balance.
- Main Function: The main function provides a menu for the user to choose between depositing, withdrawing, checking the balance, or exiting the program. It uses a
switch
statement to execute the appropriate function based on user input.
How to Run the Program
Follow these steps to run the program:
- Open a C programming environment or IDE (such as Code::Blocks, GCC, or Visual Studio).
- Create a new C file and paste the above code into the file.
- Compile the program using the command
gcc filename.c -o bank_account
(replacefilename.c
with your actual file name). - Run the compiled program using the command
./bank_account
. - Follow the on-screen menu to interact with the bank account simulator.