-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBankAccountManagement.c
183 lines (154 loc) · 5.35 KB
/
BankAccountManagement.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Structure to represent a bank account
struct BankAccount {
int accountNumber;
char accountType[20];
float balance;
float interestRate;
};
// Function prototypes
void createAccount(struct BankAccount *accounts, int *numAccounts);
void deposit(struct BankAccount *accounts, int numAccounts);
void withdraw(struct BankAccount *accounts, int numAccounts);
void checkBalance(struct BankAccount *accounts, int numAccounts);
void calculateInterest(struct BankAccount *accounts, int numAccounts);
int main() {
struct BankAccount accounts[50]; // Maximum 50 accounts
int numAccounts = 0;
int choice;
do {
printf("\nBank Account Management System\n");
printf("1. Create Account\n");
printf("2. Deposit\n");
printf("3. Withdraw\n");
printf("4. Check Balance\n");
printf("5. Calculate Interest\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
createAccount(accounts, &numAccounts);
break;
case 2:
deposit(accounts, numAccounts);
break;
case 3:
withdraw(accounts, numAccounts);
break;
case 4:
checkBalance(accounts, numAccounts);
break;
case 5:
calculateInterest(accounts, numAccounts);
break;
case 6:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 6);
return 0;
}
// Function to create a new bank account
void createAccount(struct BankAccount *accounts, int *numAccounts) {
if (*numAccounts >= 50) {
printf("Cannot create more accounts. Maximum limit reached.\n");
return;
}
printf("Enter account number: ");
scanf("%d", &accounts[*numAccounts].accountNumber);
printf("Enter account type (Savings/Current): ");
scanf(" %[^\n]", accounts[*numAccounts].accountType);
printf("Enter initial balance: ");
scanf("%f", &accounts[*numAccounts].balance);
if (strcmp(accounts[*numAccounts].accountType, "Savings") == 0) {
accounts[*numAccounts].interestRate = 5.0; // 5% interest rate for Savings account
} else if (strcmp(accounts[*numAccounts].accountType, "Current") == 0) {
accounts[*numAccounts].interestRate = 0.0; // 0% interest rate for Current account
} else {
printf("Invalid account type. Please choose Savings or Current.\n");
return;
}
(*numAccounts)++;
printf("Account created successfully.\n");
}
// Function to deposit funds into an account
void deposit(struct BankAccount *accounts, int numAccounts) {
int accNum;
float amount;
printf("Enter account number: ");
scanf("%d", &accNum);
int index = -1;
for (int i = 0; i < numAccounts; i++) {
if (accounts[i].accountNumber == accNum) {
index = i;
break;
}
}
if (index == -1) {
printf("Account not found.\n");
return;
}
printf("Enter amount to deposit: ");
scanf("%f", &amount);
accounts[index].balance += amount;
printf("Amount deposited successfully. Updated balance: %.2f\n", accounts[index].balance);
}
// Function to withdraw funds from an account
void withdraw(struct BankAccount *accounts, int numAccounts) {
int accNum;
float amount;
printf("Enter account number: ");
scanf("%d", &accNum);
int index = -1;
for (int i = 0; i < numAccounts; i++) {
if (accounts[i].accountNumber == accNum) {
index = i;
break;
}
}
if (index == -1) {
printf("Account not found.\n");
return;
}
printf("Enter amount to withdraw: ");
scanf("%f", &amount);
if (amount > accounts[index].balance) {
printf("Insufficient balance.\n");
return;
}
accounts[index].balance -= amount;
printf("Amount withdrawn successfully. Updated balance: %.2f\n", accounts[index].balance);
}
// Function to check balance of an account
void checkBalance(struct BankAccount *accounts, int numAccounts) {
int accNum;
printf("Enter account number: ");
scanf("%d", &accNum);
int index = -1;
for (int i = 0; i < numAccounts; i++) {
if (accounts[i].accountNumber == accNum) {
index = i;
break;
}
}
if (index == -1) {
printf("Account not found.\n");
return;
}
printf("Current balance: %.2f\n", accounts[index].balance);
}
// Function to calculate interest for Savings accounts
void calculateInterest(struct BankAccount *accounts, int numAccounts) {
for (int i = 0; i < numAccounts; i++) {
if (strcmp(accounts[i].accountType, "Savings") == 0) {
float interest = (accounts[i].balance * accounts[i].interestRate) / 100;
accounts[i].balance += interest;
printf("Interest calculated for account %d. Updated balance: %.2f\n", accounts[i].accountNumber, accounts[i].balance);
}
}
}