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:

  1. Open a C programming environment or IDE (such as Code::Blocks, GCC, or Visual Studio).
  2. Create a new C file and paste the above code into the file.
  3. Compile the program using the command gcc filename.c -o bank_account (replace filename.c with your actual file name).
  4. Run the compiled program using the command ./bank_account.
  5. Follow the on-screen menu to interact with the bank account simulator.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)