#include <stdio.
h>
#include <stdlib.h>
#include <string.h>
#define MAX_ACCOUNTS 100
#define NAME_LENGTH 100
#define FILE_NAME “[Link]”
// Define structure to hold account information
Struct Account {
Int accountNumber;
Char name[NAME_LENGTH];
Int age;
Char gender[10];
Char cnic[20];
Double balance;
};
// Load Accounts from File
Int loadAccounts(struct Account accounts[]) {
FILE *file = fopen(FILE_NAME, “rb”);
If (!file) return 0; // File doesn’t exist, no accounts to load
Int accountCount = 0;
While (fread(&accounts[accountCount], sizeof(struct Account), 1, file)) {
accountCount++;
Fclose(file);
Return accountCount;
// Save Accounts to File
Void saveAccounts(struct Account accounts[], int accountCount) {
FILE *file = fopen(FILE_NAME, “wb”);
If (file) {
Fwrite(accounts, sizeof(struct Account), accountCount, file);
Fclose(file);
// Check if Account Number Exists
Int accountExists(struct Account accounts[], int accountCount, int
accountNumber) {
For (int I = 0; I < accountCount; i++) {
If (accounts[i].accountNumber == accountNumber) {
Return 1; // Account number exists
Return 0;
// Create Account Function
Void createAccount(struct Account accounts[], int *accountCount) {
Struct Account newAccount;
Int accountNumber;
// Ensure unique account number
Do {
Printf(“Enter account number: “);
Scanf(“%d”, &accountNumber);
} while (accountExists(accounts, *accountCount, accountNumber));
[Link] = accountNumber;
printf(“Enter account holder name: “);
getchar(); // Consume newline from previous input
fgets([Link], NAME_LENGTH, stdin);
[Link][strcspn([Link], “\n”)] = ‘\0’; // Remove
newline
printf(“Enter age: “);
scanf(“%d”, &[Link]);
printf(“Enter gender (Male/Female): “);
getchar(); // Consume newline from previous input
fgets([Link], 10, stdin);
[Link][strcspn([Link], “\n”)] = ‘\0’; // Remove
newline
printf(“Enter CNIC number: “);
fgets([Link], 20, stdin);
[Link][strcspn([Link], “\n”)] = ‘\0’; // Remove
newline
printf(“Enter initial balance: “);
scanf(“%lf”, &[Link]);
accounts[*accountCount] = newAccount;
(*accountCount)++;
saveAccounts(accounts, *accountCount);
printf(“Account created successfully!\n”);
// View Account Function
Void viewAccount(struct Account accounts[], int accountCount) {
Int accountNumber;
Printf(“Enter account number to view: “);
Scanf(“%d”, &accountNumber);
For (int I = 0; I < accountCount; i++) {
If (accounts[i].accountNumber == accountNumber) {
Printf(“\nAccount Number: %d\n”, accounts[i].accountNumber);
Printf(“Name: %s\n”, accounts[i].name);
Printf(“Age: %d\n”, accounts[i].age);
Printf(“Gender: %s\n”, accounts[i].gender);
Printf(“CNIC: %s\n”, accounts[i].cnic);
Printf(“Balance: %.2f\n”, accounts[i].balance);
Return;
}
Printf(“Account not found!\n”);
// Update Account Function
Void updateAccount(struct Account accounts[], int accountCount) {
Int accountNumber;
Printf(“Enter account number to update: “);
Scanf(“%d”, &accountNumber);
For (int I = 0; I < accountCount; i++) {
If (accounts[i].accountNumber == accountNumber) {
Printf(“What would you like to update?\n”);
Printf(“1. Name\n”);
Printf(“2. Age\n”);
Printf(“3. Gender\n”);
Printf(“4. CNIC\n”);
Printf(“5. Balance\n”);
Printf(“Enter choice: “);
Int choice;
Scanf(“%d”, &choice);
Switch (choice) {
Case 1:
Printf(“Enter new name: “);
Getchar(); // Consume newline
Fgets(accounts[i].name, NAME_LENGTH, stdin);
Accounts[i].name[strcspn(accounts[i].name, “\n”)] = ‘\0’; //
Remove newline
Break;
Case 2:
Printf(“Enter new age: “);
Scanf(“%d”, &accounts[i].age);
Break;
Case 3:
Printf(“Enter new gender (Male/Female): “);
Getchar(); // Consume newline
Fgets(accounts[i].gender, 10, stdin);
Accounts[i].gender[strcspn(accounts[i].gender, “\n”)] = ‘\0’; //
Remove newline
Break;
Case 4:
Printf(“Enter new CNIC: “);
Getchar(); // Consume newline
Fgets(accounts[i].cnic, 20, stdin);
Accounts[i].cnic[strcspn(accounts[i].cnic, “\n”)] = ‘\0’; //
Remove newline
Break;
Case 5:
Printf(“Enter new balance: “);
Scanf(“%lf”, &accounts[i].balance);
Break;
Default:
Printf(“Invalid choice!\n”);
Return;
}
saveAccounts(accounts, accountCount);
printf(“Account updated successfully!\n”);
return;
Printf(“Account not found!\n”);
// Delete Account Function
Void deleteAccount(struct Account accounts[], int *accountCount) {
Int accountNumber;
Printf(“Enter account number to delete: “);
Scanf(“%d”, &accountNumber);
For (int I = 0; I < *accountCount; i++) {
If (accounts[i].accountNumber == accountNumber) {
// Shift all accounts after the deleted one
For (int j = I; j < *accountCount – 1; j++) {
Accounts[j] = accounts[j + 1];
(*accountCount)--; // Decrease account count
saveAccounts(accounts, *accountCount);
printf(“Account deleted successfully!\n”);
return;
}
}
Printf(“Account not found!\n”);
// Function to pause and wait for user input to continue
Void pressAnyKeyToContinue() {
Printf(“\nPress any key to continue…”);
Getchar(); // Consume any leftover newline
Getchar(); // Wait for the user to press any key
// Main Program
Int main() {
Struct Account accounts[MAX_ACCOUNTS];
Int accountCount = loadAccounts(accounts); // Load accounts from file
Int choice;
While (1) {
Printf(“----------------------“);
Printf(“\nBank Management System\n”);
Printf(“----------------------\n”);
Printf(“\n1. Create Account\n”);
Printf(“2. View Account\n”);
Printf(“3. Update Account\n”);
Printf(“4. Delete Account\n”);
Printf(“5. Exit\n”);
Printf(“Enter your choice: “);
Scanf(“%d”, &choice);
Switch (choice) {
Case 1:
createAccount(accounts, &accountCount);
pressAnyKeyToContinue(); // Pause before showing the menu again
break;
case 2:
viewAccount(accounts, accountCount);
pressAnyKeyToContinue(); // Pause before showing the menu again
break;
case 3:
updateAccount(accounts, accountCount);
pressAnyKeyToContinue(); // Pause before showing the menu again
break;
case 4:
deleteAccount(accounts, &accountCount);
pressAnyKeyToContinue(); // Pause before showing the menu again
break;
case 5:
printf(“Exiting…\n”);
return 0;
default:
printf(“Invalid choice. Try again.\n”);
pressAnyKeyToContinue(); // Pause before showing the menu again
}
}
Return 0;