0% found this document useful (0 votes)
24 views3 pages

Simple Banking System

The document is a C program that implements a simple banking system allowing users to create accounts, deposit, withdraw, and display account information. It defines a structure for account details and provides functions for each banking operation. The program runs in a loop, presenting a menu until the user chooses to exit.

Uploaded by

mikeymikemike27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

Simple Banking System

The document is a C program that implements a simple banking system allowing users to create accounts, deposit, withdraw, and display account information. It defines a structure for account details and provides functions for each banking operation. The program runs in a loop, presenting a menu until the user chooses to exit.

Uploaded by

mikeymikemike27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>

// Define constants
#define MAX_ACCOUNTS 100

// Define a structure to hold account information


typedef struct {
int accountNumber;
char name[100];
double balance;
} Account;

// Function prototypes
int createAccount(Account accounts[], int *numAccounts);
int deposit(Account accounts[], int numAccounts);
int withdraw(Account accounts[], int numAccounts);
int displayAccounts(Account accounts[], int numAccounts);
int findAccount(Account accounts[], int numAccounts, int accountNumber);

int main() {
Account accounts[MAX_ACCOUNTS];
int numAccounts = 0;
int choice;

// Main loop
do {
// Display menu
printf("\nSimple Banking System\n");
printf("1. Create Account\n");
printf("2. Deposit\n");
printf("3. Withdraw\n");
printf("4. Display Accounts\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

// Handle menu choices


switch (choice) {
case 1:
createAccount(accounts, &numAccounts);
break;
case 2:
deposit(accounts, numAccounts);
break;
case 3:
withdraw(accounts, numAccounts);
break;
case 4:
displayAccounts(accounts, numAccounts);
break;
case 5:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 5);
return 0;
}

// Function to create a new account


int createAccount(Account accounts[], int *numAccounts) {
if (*numAccounts >= MAX_ACCOUNTS) {
printf("Cannot create more accounts. Maximum limit reached.\n");
return -1;
}

Account newAccount;
newAccount.accountNumber = *numAccounts + 1;
printf("Enter name: ");
scanf("%s", newAccount.name);
newAccount.balance = 0.0;

accounts[*numAccounts] = newAccount;
(*numAccounts)++;

printf("Account created successfully. Account Number: %d\n",


newAccount.accountNumber);
return 0;
}

// Function to deposit money into an account


int deposit(Account accounts[], int numAccounts) {
int accountNumber;
double amount;

printf("Enter account number: ");


scanf("%d", &accountNumber);

int index = findAccount(accounts, numAccounts, accountNumber);


if (index == -1) {
printf("Account not found.\n");
return -1;
}

printf("Enter amount to deposit: ");


scanf("%lf", &amount);

if (amount < 0) {
printf("Invalid amount. Please enter a positive value.\n");
return -1;
}

accounts[index].balance += amount;
printf("Deposit successful. New balance: %.2f\n", accounts[index].balance);
return 0;
}

// Function to withdraw money from an account


int withdraw(Account accounts[], int numAccounts) {
int accountNumber;
double amount;

printf("Enter account number: ");


scanf("%d", &accountNumber);
int index = findAccount(accounts, numAccounts, accountNumber);
if (index == -1) {
printf("Account not found.\n");
return -1;
}

printf("Enter amount to withdraw: ");


scanf("%lf", &amount);

if (amount < 0) {
printf("Invalid amount. Please enter a positive value.\n");
return -1;
}

if (accounts[index].balance < amount) {


printf("Insufficient balance.\n");
return -1;
}

accounts[index].balance -= amount;
printf("Withdrawal successful. New balance: %.2f\n", accounts[index].balance);
return 0;
}

// Function to display all accounts


int displayAccounts(Account accounts[], int numAccounts) {
if (numAccounts == 0) {
printf("No accounts to display.\n");
return -1;
}

printf("\nAccount List:\n");
for (int i = 0; i < numAccounts; i++) {
printf("Account Number: %d, Name: %s, Balance: %.2f\n",
accounts[i].accountNumber, accounts[i].name, accounts[i].balance);
}
return 0;
}

// Function to find an account by account number


int findAccount(Account accounts[], int numAccounts, int accountNumber) {
for (int i = 0; i < numAccounts; i++) {
if (accounts[i].accountNumber == accountNumber) {
return i;
}
}
return -1;
}

You might also like