Skip to content
StudyFame

Bank Management System in C

A bank program is a great test of careful coding: money must never appear or disappear by mistake. Every withdrawal must check the balance, amounts must be positive, and every change must be saved immediately. In this project you build a bank management system that stores accounts in a binary file.

You will open accounts with automatic account numbers, deposit and withdraw money with proper validation, check balances and list all accounts. A second program calculates simple and compound interest for a fixed deposit.

What You Will Learn

  • How to design an account record.
  • How to generate the next account number from the file.
  • How to write one reusable "find account" function.
  • How to validate deposits and withdrawals.
  • How to update a balance safely in the file.
  • How to calculate simple and compound interest.

Bank Management System in C Explained

Exam definition: A bank management system maintains account records (account number, holder name, balance) and supports operations such as opening an account, deposit, withdrawal and balance enquiry. Business rules are enforced before changing data: amounts must be positive, and a withdrawal is allowed only if the balance (minus any minimum balance) is sufficient. Each change is written back to the file immediately.

Memory trick: Think like a bank cashier: (1) ask for the account number and find the account, (2) check the rule (enough money? positive amount?), (3) change the balance, (4) write it in the ledger (save to file). Find, check, change, save.

Syntax: Account Record and Rules

#define MIN_BALANCE 500.0

struct account
{
    int number;
    char name[30];
    double balance;
};

/* deposit  */ if (amount <= 0) reject;  else balance += amount;
/* withdraw */ if (amount <= 0) reject;
               else if (balance - amount < MIN_BALANCE) reject;
               else balance -= amount;

Real banking software stores money as whole paise (integers) to avoid floating-point rounding. For a learning project, double with two decimal places is fine.

Example 1: Bank Management System

#include <stdio.h>

#define FILE_NAME "bank.dat"
#define MIN_BALANCE 500.0

struct account
{
    int number;
    char name[30];
    double balance;
};

int next_account_number(void)
{
    struct account a;
    int last = 1000;
    FILE *fp = fopen(FILE_NAME, "rb");
    if (fp == NULL)
        return 1001;
    while (fread(&a, sizeof a, 1, fp) == 1)
        last = a.number;
    fclose(fp);
    return last + 1;
}

void open_account(void)
{
    struct account a;
    FILE *fp;

    a.number = next_account_number();
    printf("Holder name: ");
    scanf(" %29[^\n]", a.name);
    printf("Opening deposit (min %.0f): ", MIN_BALANCE);
    if (scanf("%lf", &a.balance) != 1 || a.balance < MIN_BALANCE)
    {
        printf("Account not opened: minimum deposit is %.2f\n", MIN_BALANCE);
        return;
    }
    fp = fopen(FILE_NAME, "ab");
    if (fp == NULL) return;
    fwrite(&a, sizeof a, 1, fp);
    fclose(fp);
    printf("Account %d opened for %s\n", a.number, a.name);
}

/* Finds an account, leaves fp positioned just after it. Returns 1 if found. */
int find_account(FILE *fp, int number, struct account *a)
{
    rewind(fp);
    while (fread(a, sizeof *a, 1, fp) == 1)
        if (a->number == number)
            return 1;
    return 0;
}

/* type: 'D' deposit, 'W' withdraw, 'B' balance */
void transaction(char type)
{
    struct account a;
    FILE *fp = fopen(FILE_NAME, "rb+");
    int number;
    double amount = 0;

    printf("Account number: ");
    if (scanf("%d", &number) != 1 || fp == NULL) { if (fp) fclose(fp); return; }
    if (!find_account(fp, number, &a))
    {
        printf("Account %d does not exist\n", number);
        fclose(fp);
        return;
    }
    if (type == 'B')
    {
        printf("%s, your balance is Rs %.2f\n", a.name, a.balance);
        fclose(fp);
        return;
    }
    printf("Amount: ");
    if (scanf("%lf", &amount) != 1 || amount <= 0)
        printf("Amount must be positive\n");
    else if (type == 'W' && a.balance - amount < MIN_BALANCE)
        printf("Insufficient balance (Rs %.2f available, Rs %.2f must remain)\n",
               a.balance, MIN_BALANCE);
    else
    {
        a.balance += (type == 'D') ? amount : -amount;
        fseek(fp, -(long) sizeof a, SEEK_CUR);
        fwrite(&a, sizeof a, 1, fp);
        printf("%s successful. New balance Rs %.2f\n",
               type == 'D' ? "Deposit" : "Withdrawal", a.balance);
    }
    fclose(fp);
}

void list_accounts(void)
{
    struct account a;
    double total = 0;
    FILE *fp = fopen(FILE_NAME, "rb");
    if (fp == NULL) { printf("No accounts\n"); return; }
    printf("%-6s %-15s %10s\n", "Acc", "Name", "Balance");
    while (fread(&a, sizeof a, 1, fp) == 1)
    {
        printf("%-6d %-15s %10.2f\n", a.number, a.name, a.balance);
        total += a.balance;
    }
    printf("Bank holds Rs %.2f in total\n", total);
    fclose(fp);
}

int main(void)
{
    int choice;
    while (1)
    {
        printf("\n1.Open 2.Deposit 3.Withdraw 4.Balance 5.List 0.Exit\nChoice: ");
        if (scanf("%d", &choice) != 1 || choice == 0)
            break;
        switch (choice)
        {
        case 1: open_account(); break;
        case 2: transaction('D'); break;
        case 3: transaction('W'); break;
        case 4: transaction('B'); break;
        case 5: list_accounts(); break;
        default: printf("Invalid choice\n");
        }
    }
    printf("Thank you for banking with us.\n");
    return 0;
}

Output

1.Open 2.Deposit 3.Withdraw 4.Balance 5.List 0.Exit
Choice: 1
Holder name: Neha Sharma
Opening deposit (min 500): 5000
Account 1001 opened for Neha Sharma

1.Open 2.Deposit 3.Withdraw 4.Balance 5.List 0.Exit
Choice: 1
Holder name: Vikram Rao
Opening deposit (min 500): 2000
Account 1002 opened for Vikram Rao

1.Open 2.Deposit 3.Withdraw 4.Balance 5.List 0.Exit
Choice: 2
Account number: 1001
Amount: 2500
Deposit successful. New balance Rs 7500.00

1.Open 2.Deposit 3.Withdraw 4.Balance 5.List 0.Exit
Choice: 3
Account number: 1001
Amount: 7500
Insufficient balance (Rs 7500.00 available, Rs 500.00 must remain)

1.Open 2.Deposit 3.Withdraw 4.Balance 5.List 0.Exit
Choice: 3
Account number: 1002
Amount: 1000
Withdrawal successful. New balance Rs 1000.00

1.Open 2.Deposit 3.Withdraw 4.Balance 5.List 0.Exit
Choice: 4
Account number: 1001
Neha Sharma, your balance is Rs 7500.00

1.Open 2.Deposit 3.Withdraw 4.Balance 5.List 0.Exit
Choice: 2
Account number: 1003
Account 1003 does not exist

1.Open 2.Deposit 3.Withdraw 4.Balance 5.List 0.Exit
Choice: 5
Acc    Name               Balance
1001   Neha Sharma        7500.00
1002   Vikram Rao         1000.00
Bank holds Rs 8500.00 in total

1.Open 2.Deposit 3.Withdraw 4.Balance 5.List 0.Exit
Choice: 0
Thank you for banking with us.

Code Explanation

  1. next_account_number() reads the last record's number and adds 1, so accounts get 1001, 1002, … automatically and never repeat.
  2. open_account() refuses an opening deposit below the minimum balance.
  3. find_account() is shared by deposit, withdraw and balance. It leaves the file position just after the found record, ready for fseek back and fwrite.
  4. One transaction() function handles three operations using a type letter, so the "find the account" and "save it" code is written only once.
  5. Withdrawing 7500 from 7500 is refused because Rs 500 must remain. Account 1003 doesn't exist, so nothing is changed.
  6. The list shows every account and the total money held, a quick check that no money appeared or vanished.

How a Withdrawal Is Processed

read account number ──► not found? ──► "does not exist"
          │ found
read amount ──► amount <= 0 ? ──► "must be positive"
          │
balance - amount < MIN ? ──► "insufficient balance"
          │ no
balance -= amount ──► fseek back ──► fwrite ──► "successful"

Every rule is checked before the balance changes. If any check fails, the file is not touched. This "validate first, then commit" order is the most important habit in programs that handle money.

Example 2: Fixed Deposit Interest Calculator

#include <stdio.h>
#include <math.h>

int main(void)
{
    double principal = 100000;      /* Rs 1,00,000 */
    double rate = 7.0;              /* % per year  */
    int year;

    printf("Principal Rs %.0f at %.1f%% per year\n", principal, rate);
    printf("Year   Simple interest   Compound (yearly)\n");
    for (year = 1; year <= 5; year++)
    {
        double si = principal * rate * year / 100.0;
        double amount = principal * pow(1 + rate / 100.0, year);
        printf("%4d   %15.2f   %17.2f\n", year, principal + si, amount);
    }
    return 0;
}

Output

Principal Rs 100000 at 7.0% per year
Year   Simple interest   Compound (yearly)
   1         107000.00           107000.00
   2         114000.00           114490.00
   3         121000.00           122504.30
   4         128000.00           131079.60
   5         135000.00           140255.17
  • Simple interest: SI = P × R × T / 100, so the amount grows by the same Rs 7,000 each year.
  • Compound interest: A = P × (1 + R/100)T; each year's interest also earns interest, so the gap grows: Rs 5,255.17 more after 5 years.
  • Compile with -lm because of pow().

Important Points to Remember

  • Validate before changing data: account exists, amount > 0, enough balance.
  • Generate unique account numbers from the last record in the file.
  • Write one find function and one transaction function instead of copying code.
  • Save every change immediately with fseek back + fwrite in "rb+" mode.
  • SI = P × R × T / 100; compound amount = P × (1 + R/100)T.
  • Real banks store money as integer paise to avoid rounding errors.

Common Mistakes Beginners Make

MistakeWhat happensFix
No balance check on withdrawalNegative balancesCheck balance - amount >= MIN_BALANCE
Accepting negative amountsA "deposit" of −500 takes money outReject amount <= 0
Asking the user for the account number on openingDuplicate account numbersGenerate it automatically
Changing the balance before validationWrong data saved when a check failsValidate first, then change
Integer division in interest: rate / 100 with intsInterest becomes 0Use 100.0 and doubles

Bank Management System Practice Questions

  1. Which checks must be done before a withdrawal?
  2. How does the program generate a new account number?
  3. Why is a single find_account() function useful?
  4. Write the formulas for simple interest and compound amount.
  5. Why do real banking systems store money as integers?
Show answers
  1. The account exists, the amount is positive, and the balance after withdrawal is at least the minimum balance.
  2. It reads the last account number in the file and adds 1 (1001 for the first account).
  3. Deposit, withdrawal and balance enquiry all need to find an account; one function avoids duplicate code and bugs.
  4. SI = P × R × T / 100; A = P × (1 + R/100)T.
  5. Floating-point numbers can't represent many decimal values exactly; integers (paise) avoid rounding errors.

Coding Exercise

Write a function int transfer(struct account *from, struct account *to, double amount) that moves money between two accounts in memory, keeping a minimum balance of 500 in the source account. Test transferring 3000 and then 5000 from an account with 6000 to an account with 1000.

Show solution
#include <stdio.h>

#define MIN_BALANCE 500.0

struct account
{
    int number;
    char name[30];
    double balance;
};

int transfer(struct account *from, struct account *to, double amount)
{
    if (amount <= 0 || from->balance - amount < MIN_BALANCE)
        return 0;
    from->balance -= amount;
    to->balance += amount;
    return 1;
}

int main(void)
{
    struct account a = {1001, "Neha", 6000}, b = {1002, "Vikram", 1000};
    double amounts[] = {3000, 5000};
    int i;

    for (i = 0; i < 2; i++)
    {
        if (transfer(&a, &b, amounts[i]))
            printf("Transferred %.0f -> Neha %.2f, Vikram %.2f\n", amounts[i], a.balance, b.balance);
        else
            printf("Transfer of %.0f refused (Neha has %.2f)\n", amounts[i], a.balance);
    }
    return 0;
}
Transferred 3000 -> Neha 3000.00, Vikram 4000.00
Transfer of 5000 refused (Neha has 3000.00)

Both balances change together or not at all. In databases this all-or-nothing rule is called a transaction.

Quick Quiz: Bank Management System (MCQ)

  1. Before a withdrawal, the program must first:

    A) Subtract the amount   B) Validate the amount and balance   C) Close the file   D) Print the list

  2. Simple interest on Rs 1000 at 10% for 2 years is:

    A) Rs 100   B) Rs 200   C) Rs 210   D) Rs 1200

  3. Compound amount on Rs 1000 at 10% for 2 years (yearly) is:

    A) Rs 1200   B) Rs 1210   C) Rs 1100   D) Rs 2000

  4. Which file mode is used to update a balance?

    A) "wb"   B) "ab"   C) "rb+"   D) "r"

  5. A deposit of a negative amount should be:

    A) Accepted   B) Rejected   C) Converted to positive silently   D) Added twice

Show answers
  1. B: validate the amount and balance.
  2. B: Rs 200.
  3. B: Rs 1210.
  4. C: "rb+".
  5. B: rejected.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

Account recordnumber, name, balance
Cashier stepsFind, check, change, save
Withdraw rulebalance − amount ≥ minimum
Amount ruleMust be > 0
New account numberLast number + 1
Simple interestP × R × T / 100
Compound amountP × (1 + R/100)T

Frequently asked questions

What features does a bank management system in C have?
Open account, deposit, withdraw, balance enquiry and list accounts, with data stored in a file.
How do you prevent overdrawing an account?
Check that balance − amount is not below the minimum balance before subtracting.
What is the difference between simple and compound interest?
Simple interest is on the principal only; compound interest is also earned on previous interest.

Remember

  • Be the cashier: find, check, change, save.
  • Validate first; never let money appear or vanish.

Next Topic

Next lesson: Contact Book Project in C. You will build a phonebook that adds, searches, edits and deletes contacts stored in a file.