├── .github └── workflows │ └── c-cpp.yml ├── Authentication-System.c ├── OTP-Login-system.c ├── README.md ├── banking-management-system.c ├── cover.jpg ├── dbms-software.c ├── file-compression-utility.c ├── library-management-system.c ├── number-gusseing-game.c ├── snake-game.c ├── student-grade-system.c └── voting-system.c /.github/workflows/c-cpp.yml: -------------------------------------------------------------------------------- 1 | name: C/C++ CI 2 | 3 | on: 4 | push: 5 | branches: [ "main" ] 6 | pull_request: 7 | branches: [ "main" ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v4 16 | - name: configure 17 | run: ./configure 18 | - name: make 19 | run: make 20 | - name: make check 21 | run: make check 22 | - name: make distcheck 23 | run: make distcheck 24 | -------------------------------------------------------------------------------- /Authentication-System.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define MAX_USERS 50 6 | #define MAX_USERNAME 50 7 | #define MAX_PASSWORD 50 8 | 9 | struct User { 10 | char username[MAX_USERNAME]; 11 | char password[MAX_PASSWORD]; 12 | }; 13 | 14 | struct User users[MAX_USERS]; 15 | int numUsers = 0; 16 | 17 | void signup() { 18 | if (numUsers >= MAX_USERS) { 19 | printf("Cannot add more users. Maximum user limit reached.\n"); 20 | return; 21 | } 22 | 23 | printf("Enter username: "); 24 | scanf("%s", users[numUsers].username); 25 | 26 | printf("Enter password: "); 27 | scanf("%s", users[numUsers].password); 28 | 29 | numUsers++; 30 | printf("User signed up successfully!\n"); 31 | } 32 | 33 | int login() { 34 | char username[MAX_USERNAME], password[MAX_PASSWORD]; 35 | printf("Enter username: "); 36 | scanf("%s", username); 37 | 38 | printf("Enter password: "); 39 | scanf("%s", password); 40 | 41 | for (int i = 0; i < numUsers; i++) { 42 | if (strcmp(username, users[i].username) == 0 && strcmp(password, users[i].password) == 0) { 43 | printf("Login successful!\n"); 44 | return 1; // Return 1 for successful login 45 | } 46 | } 47 | 48 | printf("Invalid username or password. Login failed.\n"); 49 | return 0; // Return 0 for failed login 50 | } 51 | 52 | int main() { 53 | int choice; 54 | do { 55 | printf("1. Signup\n"); 56 | printf("2. Login\n"); 57 | printf("3. Exit\n"); 58 | printf("Enter your choice: "); 59 | scanf("%d", &choice); 60 | 61 | switch (choice) { 62 | case 1: 63 | signup(); 64 | break; 65 | case 2: 66 | if (login()) { 67 | // Continue to user's menu or other functionalities 68 | printf("Welcome to the system!\n"); 69 | } 70 | break; 71 | case 3: 72 | printf("Exiting...\n"); 73 | break; 74 | default: 75 | printf("Invalid choice. Please try again.\n"); 76 | break; 77 | } 78 | } while (choice != 3); 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /OTP-Login-system.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #define MAX_USERS 50 6 | #define MAX_PHONE_NUMBER 11 // Including space for null terminator 7 | 8 | struct User { 9 | char phoneNumber[MAX_PHONE_NUMBER]; 10 | int otp; 11 | int otpVerified; 12 | }; 13 | 14 | struct User users[MAX_USERS]; 15 | int numUsers = 0; 16 | 17 | void signup() { 18 | if (numUsers >= MAX_USERS) { 19 | printf("Cannot add more users. Maximum user limit reached.\n"); 20 | return; 21 | } 22 | 23 | printf("Enter phone number (10 digits): "); 24 | scanf("%s", users[numUsers].phoneNumber); 25 | 26 | users[numUsers].otpVerified = 0; // OTP not verified initially 27 | numUsers++; 28 | printf("User signed up successfully!\n"); 29 | } 30 | 31 | void generateOTP(struct User *user) { 32 | srand(time(NULL)); 33 | user->otp = rand() % 9000 + 1000; // Generate a 4-digit OTP 34 | printf("OTP for login: %d\n", user->otp); // Display OTP in terminal 35 | } 36 | 37 | int login() { 38 | char phoneNumber[MAX_PHONE_NUMBER]; 39 | printf("Enter phone number (10 digits): "); 40 | scanf("%s", phoneNumber); 41 | 42 | for (int i = 0; i < numUsers; i++) { 43 | if (strcmp(phoneNumber, users[i].phoneNumber) == 0) { 44 | generateOTP(&users[i]); // Generate OTP for the logged-in user 45 | printf("Enter OTP: "); 46 | int otp; 47 | scanf("%d", &otp); 48 | 49 | if (otp == users[i].otp) { 50 | users[i].otpVerified = 1; // OTP verified 51 | printf("OTP verification successful!\n"); 52 | return 1; // Return 1 for successful login 53 | } else { 54 | printf("Incorrect OTP. Login failed.\n"); 55 | return 0; // Return 0 for failed login 56 | } 57 | } 58 | } 59 | 60 | printf("Phone number not found. Login failed.\n"); 61 | return 0; // Return 0 for failed login 62 | } 63 | 64 | int main() { 65 | int choice; 66 | do { 67 | printf("1. Signup\n"); 68 | printf("2. Login\n"); 69 | printf("3. Exit\n"); 70 | printf("Enter your choice: "); 71 | scanf("%d", &choice); 72 | 73 | switch (choice) { 74 | case 1: 75 | signup(); 76 | break; 77 | case 2: 78 | if (login()) { 79 | // Continue to user's menu or other functionalities 80 | printf("Welcome to the system!\n"); 81 | } 82 | break; 83 | case 3: 84 | printf("Exiting...\n"); 85 | break; 86 | default: 87 | printf("Invalid choice. Please try again.\n"); 88 | break; 89 | } 90 | } while (choice != 3); 91 | 92 | return 0; 93 | } 94 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 | ![GitHub repo size](https://img.shields.io/github/repo-size/codeaashu/C-Projects) 4 | ![GitHub stars](https://img.shields.io/github/stars/codeaashu/C-Projects?style=social) 5 | ![GitHub forks](https://img.shields.io/github/forks/codeaashu/C-Projects?style=social) 6 | [![Twitter Follow](https://img.shields.io/twitter/follow/warrior_aashuu?style=social)](https://twitter.com/intent/follow?screen_name=warrior_aashuu) 7 | 8 |
9 |

C Programming Learning Repository

10 | 11 | 12 | 13 |

C Programming Projects

14 | 15 | Welcome to my C programming learning repository! This repository contains various programs and projects implemented in C to enhance programming skills. 16 | 17 |
18 | 19 | ## 20 | ## Project Documentation 21 | 22 | ### 1. Voting System 23 | 24 | - Description: A program to simulate a voting system. 25 | - Blog Post: [Voting System](https://codeaashu.hashnode.dev/building-a-voting-system-in-c) 26 | 27 | ### 2. Student Grade System 28 | 29 | - Description: A program to track and manage student grades. 30 | - Blog Post: [Student Grade System](https://codeaashu.hashnode.dev/building-a-student-grade-tracker-in-c) 31 | 32 | ### 3. Snake Game 33 | 34 | - Description: Implementation of the classic Snake game. 35 | - Blog Post: [Snake Game](https://codeaashu.hashnode.dev/creating-a-simple-snake-game-in-c) 36 | 37 | ### 4. OTP Login System 38 | 39 | - Description: A secure login system using OTP authentication. 40 | - Blog Post: [OTP Login System](https://codeaashu.hashnode.dev/building-an-otp-based-login-system-in-c) 41 | 42 | ### 5. Number Guessing Game 43 | 44 | - Description: A simple number guessing game. 45 | - Blog Post: [Number Guessing Game](https://codeaashu.hashnode.dev/building-a-number-guessing-game-in-c) 46 | 47 | ### 6. Library Management System 48 | 49 | - Description: A program to manage library resources. 50 | - Blog Post: [Library Management System](https://codeaashu.hashnode.dev/building-a-library-management-system-in-c) 51 | 52 | ### 7. File Compression Utility 53 | 54 | - Description: Implementation of a file compression utility using algorithms like Huffman coding or Run-Length Encoding. 55 | - Blog Post: [File Compression Utility](https://codeaashu.hashnode.dev/understanding-huffman-coding-in-c) 56 | 57 | ### 8. DBMS Software 58 | 59 | - Description: A basic database management system. 60 | - Blog Post: [DBMS Software](https://codeaashu.hashnode.dev/simple-database-management-in-c) 61 | 62 | ### 9. Banking Management System 63 | 64 | - Description: A program to manage banking operations like account creation, transactions, etc. 65 | - Blog Post: [Banking Management System](https://codeaashu.hashnode.dev/building-a-basic-banking-system-in-c) 66 | 67 | ### 10. Authentication System 68 | 69 | - Description: A user authentication system. 70 | - Blog Post: [Authentication System](https://codeaashu.hashnode.dev/basic-user-authentication-system-in-c) 71 | 72 | ## Usage 73 | 74 | To run these programs, you can compile them using a C compiler like GCC and execute the generated binaries. 75 | -------------------------------------------------------------------------------- /banking-management-system.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define MAX_ACCOUNTS 100 6 | #define MAX_TRANSACTIONS 100 7 | 8 | struct Transaction { 9 | char type[20]; // "Deposit", "Withdrawal", "Transfer" 10 | float amount; 11 | }; 12 | 13 | struct Account { 14 | int accountNumber; 15 | char name[50]; 16 | float balance; 17 | struct Transaction transactions[MAX_TRANSACTIONS]; 18 | int numTransactions; 19 | }; 20 | 21 | struct Account accounts[MAX_ACCOUNTS]; 22 | int numAccounts = 0; 23 | 24 | int findAccount(int accountNumber) { 25 | for (int i = 0; i < numAccounts; i++) { 26 | if (accounts[i].accountNumber == accountNumber) { 27 | return i; 28 | } 29 | } 30 | return -1; 31 | } 32 | 33 | void createAccount() { 34 | if (numAccounts >= MAX_ACCOUNTS) { 35 | printf("Maximum number of accounts reached!\n"); 36 | return; 37 | } 38 | 39 | struct Account newAccount; 40 | newAccount.accountNumber = numAccounts + 1; 41 | printf("Enter account holder's name: "); 42 | scanf("%s", newAccount.name); 43 | newAccount.balance = 0.0; 44 | newAccount.numTransactions = 0; 45 | 46 | accounts[numAccounts++] = newAccount; 47 | printf("Account created successfully. Account Number: %d\n", newAccount.accountNumber); 48 | } 49 | 50 | void deposit(int accountNumber, float amount) { 51 | int index = findAccount(accountNumber); 52 | if (index != -1) { 53 | accounts[index].balance += amount; 54 | strcpy(accounts[index].transactions[accounts[index].numTransactions].type, "Deposit"); 55 | accounts[index].transactions[accounts[index].numTransactions].amount = amount; 56 | accounts[index].numTransactions++; 57 | printf("Deposit successful. Current balance: %.2f\n", accounts[index].balance); 58 | } else { 59 | printf("Account not found!\n"); 60 | } 61 | } 62 | 63 | void withdraw(int accountNumber, float amount) { 64 | int index = findAccount(accountNumber); 65 | if (index != -1) { 66 | if (accounts[index].balance >= amount) { 67 | accounts[index].balance -= amount; 68 | strcpy(accounts[index].transactions[accounts[index].numTransactions].type, "Withdrawal"); 69 | accounts[index].transactions[accounts[index].numTransactions].amount = amount; 70 | accounts[index].numTransactions++; 71 | printf("Withdrawal successful. Current balance: %.2f\n", accounts[index].balance); 72 | } else { 73 | printf("Insufficient balance!\n"); 74 | } 75 | } else { 76 | printf("Account not found!\n"); 77 | } 78 | } 79 | 80 | void transfer(int fromAccount, int toAccount, float amount) { 81 | int fromIndex = findAccount(fromAccount); 82 | int toIndex = findAccount(toAccount); 83 | 84 | if (fromIndex != -1 && toIndex != -1) { 85 | if (accounts[fromIndex].balance >= amount) { 86 | accounts[fromIndex].balance -= amount; 87 | accounts[toIndex].balance += amount; 88 | 89 | strcpy(accounts[fromIndex].transactions[accounts[fromIndex].numTransactions].type, "Transfer (To)"); 90 | accounts[fromIndex].transactions[accounts[fromIndex].numTransactions].amount = amount; 91 | accounts[fromIndex].numTransactions++; 92 | 93 | strcpy(accounts[toIndex].transactions[accounts[toIndex].numTransactions].type, "Transfer (From)"); 94 | accounts[toIndex].transactions[accounts[toIndex].numTransactions].amount = amount; 95 | accounts[toIndex].numTransactions++; 96 | 97 | printf("Transfer successful. Current balances:\n"); 98 | printf("From Account %d: %.2f\n", fromAccount, accounts[fromIndex].balance); 99 | printf("To Account %d: %.2f\n", toAccount, accounts[toIndex].balance); 100 | } else { 101 | printf("Insufficient balance in the sender's account!\n"); 102 | } 103 | } else { 104 | printf("One or both accounts not found!\n"); 105 | } 106 | } 107 | 108 | void viewTransactions(int accountNumber) { 109 | int index = findAccount(accountNumber); 110 | if (index != -1) { 111 | printf("Transaction history for Account %d:\n", accountNumber); 112 | for (int i = 0; i < accounts[index].numTransactions; i++) { 113 | printf("%d. Type: %s, Amount: %.2f\n", i + 1, accounts[index].transactions[i].type, accounts[index].transactions[i].amount); 114 | } 115 | } else { 116 | printf("Account not found!\n"); 117 | } 118 | } 119 | 120 | int main() { 121 | int choice, accountNumber, toAccount; 122 | float amount; 123 | 124 | do { 125 | printf("\nBanking System Menu:\n"); 126 | printf("1. Create Account\n"); 127 | printf("2. Deposit\n"); 128 | printf("3. Withdraw\n"); 129 | printf("4. Transfer\n"); 130 | printf("5. View Transactions\n"); 131 | printf("6. Exit\n"); 132 | printf("Enter your choice: "); 133 | scanf("%d", &choice); 134 | 135 | switch (choice) { 136 | case 1: 137 | createAccount(); 138 | break; 139 | case 2: 140 | printf("Enter account number: "); 141 | scanf("%d", &accountNumber); 142 | printf("Enter amount to deposit: "); 143 | scanf("%f", &amount); 144 | deposit(accountNumber, amount); 145 | break; 146 | case 3: 147 | printf("Enter account number: "); 148 | scanf("%d", &accountNumber); 149 | printf("Enter amount to withdraw: "); 150 | scanf("%f", &amount); 151 | withdraw(accountNumber, amount); 152 | break; 153 | case 4: 154 | printf("Enter sender's account number: "); 155 | scanf("%d", &accountNumber); 156 | printf("Enter recipient's account number: "); 157 | scanf("%d", &toAccount); 158 | printf("Enter amount to transfer: "); 159 | scanf("%f", &amount); 160 | transfer(accountNumber, toAccount, amount); 161 | break; 162 | case 5: 163 | printf("Enter account number: "); 164 | scanf("%d", &accountNumber); 165 | viewTransactions(accountNumber); 166 | break; 167 | case 6: 168 | printf("Exiting program.\n"); 169 | break; 170 | default: 171 | printf("Invalid choice. Please try again.\n"); 172 | } 173 | } while (choice != 6); 174 | 175 | return 0; 176 | } 177 | -------------------------------------------------------------------------------- /cover.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeaashu/C-Projects/4cf3ab852c12a419118f2781583efd85d8bc79ef/cover.jpg -------------------------------------------------------------------------------- /dbms-software.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define MAX_TABLES 10 6 | #define MAX_ROWS 100 7 | #define MAX_COLUMNS 10 8 | #define MAX_COL_NAME_LEN 50 9 | #define MAX_DATA_LEN 100 10 | 11 | struct Column { 12 | char name[MAX_COL_NAME_LEN]; 13 | }; 14 | 15 | struct Table { 16 | char name[MAX_COL_NAME_LEN]; 17 | struct Column columns[MAX_COLUMNS]; 18 | int numColumns; 19 | char data[MAX_ROWS][MAX_COLUMNS][MAX_DATA_LEN]; 20 | int numRows; 21 | }; 22 | 23 | struct Table tables[MAX_TABLES]; 24 | int numTables = 0; 25 | 26 | int findTableIndex(const char* tableName) { 27 | for (int i = 0; i < numTables; i++) { 28 | if (strcmp(tables[i].name, tableName) == 0) { 29 | return i; 30 | } 31 | } 32 | return -1; 33 | } 34 | 35 | void createTable() { 36 | if (numTables >= MAX_TABLES) { 37 | printf("Maximum number of tables reached!\n"); 38 | return; 39 | } 40 | 41 | struct Table newTable; 42 | printf("Enter table name: "); 43 | scanf("%s", newTable.name); 44 | printf("Enter number of columns: "); 45 | scanf("%d", &newTable.numColumns); 46 | 47 | for (int i = 0; i < newTable.numColumns; i++) { 48 | printf("Enter column %d name: ", i + 1); 49 | scanf("%s", newTable.columns[i].name); 50 | } 51 | 52 | newTable.numRows = 0; 53 | tables[numTables++] = newTable; 54 | printf("Table created successfully.\n"); 55 | } 56 | 57 | void insertData() { 58 | char tableName[MAX_COL_NAME_LEN]; 59 | printf("Enter table name to insert data: "); 60 | scanf("%s", tableName); 61 | 62 | int tableIndex = findTableIndex(tableName); 63 | if (tableIndex != -1) { 64 | if (tables[tableIndex].numRows < MAX_ROWS) { 65 | printf("Enter data for each column:\n"); 66 | for (int i = 0; i < tables[tableIndex].numColumns; i++) { 67 | printf("Enter %s: ", tables[tableIndex].columns[i].name); 68 | scanf("%s", tables[tableIndex].data[tables[tableIndex].numRows][i]); 69 | } 70 | tables[tableIndex].numRows++; 71 | printf("Data inserted successfully.\n"); 72 | } else { 73 | printf("Maximum rows limit reached for this table.\n"); 74 | } 75 | } else { 76 | printf("Table not found!\n"); 77 | } 78 | } 79 | 80 | void selectData() { 81 | char tableName[MAX_COL_NAME_LEN]; 82 | printf("Enter table name to select data: "); 83 | scanf("%s", tableName); 84 | 85 | int tableIndex = findTableIndex(tableName); 86 | if (tableIndex != -1) { 87 | printf("Data in table %s:\n", tableName); 88 | printf("--------------------------------------------------\n"); 89 | printf("|"); 90 | for (int i = 0; i < tables[tableIndex].numColumns; i++) { 91 | printf(" %-20s|", tables[tableIndex].columns[i].name); 92 | } 93 | printf("\n--------------------------------------------------\n"); 94 | 95 | for (int i = 0; i < tables[tableIndex].numRows; i++) { 96 | printf("|"); 97 | for (int j = 0; j < tables[tableIndex].numColumns; j++) { 98 | printf(" %-20s|", tables[tableIndex].data[i][j]); 99 | } 100 | printf("\n"); 101 | } 102 | printf("--------------------------------------------------\n"); 103 | } else { 104 | printf("Table not found!\n"); 105 | } 106 | } 107 | 108 | int main() { 109 | int choice; 110 | do { 111 | printf("\nDatabase Management System Menu:\n"); 112 | printf("1. Create Table\n"); 113 | printf("2. Insert Data\n"); 114 | printf("3. Select Data\n"); 115 | printf("4. Exit\n"); 116 | printf("Enter your choice: "); 117 | scanf("%d", &choice); 118 | 119 | switch (choice) { 120 | case 1: 121 | createTable(); 122 | break; 123 | case 2: 124 | insertData(); 125 | break; 126 | case 3: 127 | selectData(); 128 | break; 129 | case 4: 130 | printf("Exiting program.\n"); 131 | break; 132 | default: 133 | printf("Invalid choice. Please try again.\n"); 134 | } 135 | } while (choice != 4); 136 | 137 | return 0; 138 | } 139 | -------------------------------------------------------------------------------- /file-compression-utility.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct Node { 6 | char data; 7 | int frequency; 8 | struct Node *left; 9 | struct Node *right; 10 | }; 11 | 12 | struct MinHeap { 13 | int size; 14 | int capacity; 15 | struct Node **array; 16 | }; 17 | 18 | struct Node* createNode(char data, int frequency) { 19 | struct Node* node = (struct Node*)malloc(sizeof(struct Node)); 20 | node->data = data; 21 | node->frequency = frequency; 22 | node->left = node->right = NULL; 23 | return node; 24 | } 25 | 26 | struct MinHeap* createMinHeap(int capacity) { 27 | struct MinHeap* minHeap = (struct MinHeap*)malloc(sizeof(struct MinHeap)); 28 | minHeap->size = 0; 29 | minHeap->capacity = capacity; 30 | minHeap->array = (struct Node**)malloc(capacity * sizeof(struct Node*)); 31 | return minHeap; 32 | } 33 | 34 | void swapNode(struct Node** a, struct Node** b) { 35 | struct Node* temp = *a; 36 | *a = *b; 37 | *b = temp; 38 | } 39 | 40 | void minHeapify(struct MinHeap* minHeap, int idx) { 41 | int smallest = idx; 42 | int left = 2 * idx + 1; 43 | int right = 2 * idx + 2; 44 | 45 | if (left < minHeap->size && minHeap->array[left]->frequency < minHeap->array[smallest]->frequency) 46 | smallest = left; 47 | if (right < minHeap->size && minHeap->array[right]->frequency < minHeap->array[smallest]->frequency) 48 | smallest = right; 49 | 50 | if (smallest != idx) { 51 | swapNode(&minHeap->array[smallest], &minHeap->array[idx]); 52 | minHeapify(minHeap, smallest); 53 | } 54 | } 55 | 56 | int isSizeOne(struct MinHeap* minHeap) { 57 | return (minHeap->size == 1); 58 | } 59 | 60 | struct Node* extractMin(struct MinHeap* minHeap) { 61 | struct Node* temp = minHeap->array[0]; 62 | minHeap->array[0] = minHeap->array[minHeap->size - 1]; 63 | --minHeap->size; 64 | minHeapify(minHeap, 0); 65 | return temp; 66 | } 67 | 68 | void insertMinHeap(struct MinHeap* minHeap, struct Node* node) { 69 | ++minHeap->size; 70 | int i = minHeap->size - 1; 71 | while (i && node->frequency < minHeap->array[(i - 1) / 2]->frequency) { 72 | minHeap->array[i] = minHeap->array[(i - 1) / 2]; 73 | i = (i - 1) / 2; 74 | } 75 | minHeap->array[i] = node; 76 | } 77 | 78 | void buildMinHeap(struct MinHeap* minHeap) { 79 | int n = minHeap->size - 1; 80 | int i; 81 | for (i = (n - 1) / 2; i >= 0; --i) { 82 | minHeapify(minHeap, i); 83 | } 84 | } 85 | 86 | struct MinHeap* createAndBuildMinHeap(char data[], int frequency[], int size) { 87 | struct MinHeap* minHeap = createMinHeap(size); 88 | for (int i = 0; i < size; ++i) 89 | minHeap->array[i] = createNode(data[i], frequency[i]); 90 | minHeap->size = size; 91 | buildMinHeap(minHeap); 92 | return minHeap; 93 | } 94 | 95 | struct Node* buildHuffmanTree(char data[], int frequency[], int size) { 96 | struct Node *left, *right, *top; 97 | struct MinHeap* minHeap = createAndBuildMinHeap(data, frequency, size); 98 | while (!isSizeOne(minHeap)) { 99 | left = extractMin(minHeap); 100 | right = extractMin(minHeap); 101 | top = createNode('$', left->frequency + right->frequency); 102 | top->left = left; 103 | top->right = right; 104 | insertMinHeap(minHeap, top); 105 | } 106 | return extractMin(minHeap); 107 | } 108 | 109 | void printCodes(struct Node* root, int arr[], int top) { 110 | if (root->left) { 111 | arr[top] = 0; 112 | printCodes(root->left, arr, top + 1); 113 | } 114 | if (root->right) { 115 | arr[top] = 1; 116 | printCodes(root->right, arr, top + 1); 117 | } 118 | if (!root->left && !root->right) { 119 | printf("%c: ", root->data); 120 | for (int i = 0; i < top; ++i) 121 | printf("%d", arr[i]); 122 | printf("\n"); 123 | } 124 | } 125 | 126 | void huffmanCodes(char data[], int frequency[], int size) { 127 | struct Node* root = buildHuffmanTree(data, frequency, size); 128 | int arr[size], top = 0; 129 | printCodes(root, arr, top); 130 | } 131 | 132 | int main() { 133 | char data[] = {'a', 'b', 'c', 'd', 'e', 'f'}; 134 | int frequency[] = {5, 9, 12, 13, 16, 45}; 135 | int size = sizeof(data) / sizeof(data[0]); 136 | huffmanCodes(data, frequency, size); 137 | return 0; 138 | } 139 | -------------------------------------------------------------------------------- /library-management-system.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define MAX_BOOKS 100 6 | #define MAX_NAME_LENGTH 50 7 | #define MAX_ID_LENGTH 10 8 | 9 | typedef struct { 10 | char id[MAX_ID_LENGTH]; 11 | char title[MAX_NAME_LENGTH]; 12 | char author[MAX_NAME_LENGTH]; 13 | int available; 14 | } Book; 15 | 16 | Book books[MAX_BOOKS]; 17 | int numBooks = 0; 18 | 19 | void addBook() { 20 | if (numBooks >= MAX_BOOKS) { 21 | printf("Cannot add more books. Library is full.\n"); 22 | return; 23 | } 24 | 25 | Book newBook; 26 | printf("Enter book ID: "); 27 | scanf("%s", newBook.id); 28 | printf("Enter book title: "); 29 | scanf(" %[^\n]s", newBook.title); 30 | printf("Enter author name: "); 31 | scanf(" %[^\n]s", newBook.author); 32 | newBook.available = 1; // Assume new books are available 33 | books[numBooks++] = newBook; 34 | printf("Book added successfully.\n"); 35 | } 36 | 37 | void displayBooks() { 38 | printf("ID\tTitle\tAuthor\tAvailable\n"); 39 | for (int i = 0; i < numBooks; i++) { 40 | printf("%s\t%s\t%s\t%s\n", books[i].id, books[i].title, books[i].author, 41 | (books[i].available == 1) ? "Yes" : "No"); 42 | } 43 | } 44 | 45 | void deleteBook() { 46 | char idToDelete[MAX_ID_LENGTH]; 47 | printf("Enter book ID to delete: "); 48 | scanf("%s", idToDelete); 49 | 50 | for (int i = 0; i < numBooks; i++) { 51 | if (strcmp(books[i].id, idToDelete) == 0) { 52 | // Shift remaining books to fill the gap 53 | for (int j = i; j < numBooks - 1; j++) { 54 | books[j] = books[j + 1]; 55 | } 56 | numBooks--; 57 | printf("Book deleted successfully.\n"); 58 | return; 59 | } 60 | } 61 | printf("Book not found.\n"); 62 | } 63 | 64 | void updateBook() { 65 | char idToUpdate[MAX_ID_LENGTH]; 66 | printf("Enter book ID to update: "); 67 | scanf("%s", idToUpdate); 68 | 69 | for (int i = 0; i < numBooks; i++) { 70 | if (strcmp(books[i].id, idToUpdate) == 0) { 71 | printf("Enter new title: "); 72 | scanf(" %[^\n]s", books[i].title); 73 | printf("Enter new author: "); 74 | scanf(" %[^\n]s", books[i].author); 75 | printf("Book updated successfully.\n"); 76 | return; 77 | } 78 | } 79 | printf("Book not found.\n"); 80 | } 81 | 82 | void issueBook() { 83 | char idToIssue[MAX_ID_LENGTH]; 84 | printf("Enter book ID to issue: "); 85 | scanf("%s", idToIssue); 86 | 87 | for (int i = 0; i < numBooks; i++) { 88 | if (strcmp(books[i].id, idToIssue) == 0) { 89 | if (books[i].available == 1) { 90 | books[i].available = 0; // Mark as not available 91 | printf("Book issued successfully.\n"); 92 | } else { 93 | printf("Book is already issued.\n"); 94 | } 95 | return; 96 | } 97 | } 98 | printf("Book not found.\n"); 99 | } 100 | 101 | void returnBook() { 102 | char idToReturn[MAX_ID_LENGTH]; 103 | printf("Enter book ID to return: "); 104 | scanf("%s", idToReturn); 105 | 106 | for (int i = 0; i < numBooks; i++) { 107 | if (strcmp(books[i].id, idToReturn) == 0) { 108 | if (books[i].available == 0) { 109 | books[i].available = 1; // Mark as available 110 | printf("Book returned successfully.\n"); 111 | } else { 112 | printf("Book is already available.\n"); 113 | } 114 | return; 115 | } 116 | } 117 | printf("Book not found.\n"); 118 | } 119 | 120 | void generateReport() { 121 | int availableBooks = 0; 122 | for (int i = 0; i < numBooks; i++) { 123 | if (books[i].available == 1) { 124 | availableBooks++; 125 | } 126 | } 127 | printf("Total books: %d\n", numBooks); 128 | printf("Available books: %d\n", availableBooks); 129 | printf("Issued books: %d\n", numBooks - availableBooks); 130 | } 131 | 132 | int main() { 133 | int choice; 134 | do { 135 | printf("\nLibrary Management System Menu:\n"); 136 | printf("1. Add Book\n"); 137 | printf("2. Display Books\n"); 138 | printf("3. Delete Book\n"); 139 | printf("4. Update Book\n"); 140 | printf("5. Issue Book\n"); 141 | printf("6. Return Book\n"); 142 | printf("7. Generate Report\n"); 143 | printf("0. Exit\n"); 144 | printf("Enter your choice: "); 145 | scanf("%d", &choice); 146 | 147 | switch (choice) { 148 | case 1: 149 | addBook(); 150 | break; 151 | case 2: 152 | displayBooks(); 153 | break; 154 | case 3: 155 | deleteBook(); 156 | break; 157 | case 4: 158 | updateBook(); 159 | break; 160 | case 5: 161 | issueBook(); 162 | break; 163 | case 6: 164 | returnBook(); 165 | break; 166 | case 7: 167 | generateReport(); 168 | break; 169 | case 0: 170 | printf("Exiting program.\n"); 171 | break; 172 | default: 173 | printf("Invalid choice. Please try again.\n"); 174 | } 175 | } while (choice != 0); 176 | 177 | return 0; 178 | } 179 | -------------------------------------------------------------------------------- /number-gusseing-game.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | int number, guess, attempts = 0; 7 | srand(time(0)); // Seed the random number generator 8 | 9 | number = rand() % 100 + 1; // Generate a random number between 1 and 100 10 | 11 | printf("Welcome to the Guessing Game!\n"); 12 | printf("Guess a number between 1 and 100\n"); 13 | 14 | do { 15 | printf("Enter your guess: "); 16 | scanf("%d", &guess); 17 | attempts++; 18 | 19 | if (guess > number) { 20 | printf("Too high! Try again.\n"); 21 | } else if (guess < number) { 22 | printf("Too low! Try again.\n"); 23 | } else { 24 | printf("Congratulations! You guessed the number in %d attempts.\n", attempts); 25 | } 26 | } while (guess != number); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /snake-game.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define WIDTH 20 7 | #define HEIGHT 10 8 | 9 | int snakeX[100], snakeY[100], snakeLength; 10 | int foodX, foodY; 11 | int direction; // 1 for left, 2 for right, 3 for up, 4 for down 12 | int gameover; 13 | 14 | void setup() { 15 | snakeLength = 1; 16 | snakeX[0] = WIDTH / 2; 17 | snakeY[0] = HEIGHT / 2; 18 | direction = 2; // Start moving right 19 | gameover = 0; 20 | foodX = rand() % WIDTH; 21 | foodY = rand() % HEIGHT; 22 | } 23 | 24 | void draw() { 25 | system("clear"); 26 | int i, j; 27 | for (i = 0; i < HEIGHT + 2; i++) { 28 | for (j = 0; j < WIDTH + 2; j++) { 29 | if (i == 0 || i == HEIGHT + 1 || j == 0 || j == WIDTH + 1) { 30 | printf("#"); // Draw walls 31 | } else if (i == foodY && j == foodX) { 32 | printf("F"); // Draw food 33 | } else { 34 | int isSnakePart = 0; 35 | for (int k = 0; k < snakeLength; k++) { 36 | if (snakeX[k] == j && snakeY[k] == i) { 37 | printf("O"); // Draw snake 38 | isSnakePart = 1; 39 | break; 40 | } 41 | } 42 | if (!isSnakePart) { 43 | printf(" "); 44 | } 45 | } 46 | } 47 | printf("\n"); 48 | } 49 | printf("Use WASD or arrow keys to move the snake. Press q to quit.\n"); 50 | } 51 | 52 | void input() { 53 | char key; 54 | scanf("%c", &key); 55 | 56 | switch (key) { 57 | case 'w': 58 | case 'W': 59 | case 72: // Up arrow key 60 | if (direction != 4) 61 | direction = 3; 62 | break; 63 | case 's': 64 | case 'S': 65 | case 80: // Down arrow key 66 | if (direction != 3) 67 | direction = 4; 68 | break; 69 | case 'a': 70 | case 'A': 71 | case 75: // Left arrow key 72 | if (direction != 2) 73 | direction = 1; 74 | break; 75 | case 'd': 76 | case 'D': 77 | case 77: // Right arrow key 78 | if (direction != 1) 79 | direction = 2; 80 | break; 81 | case 'q': 82 | case 'Q': 83 | gameover = 1; 84 | break; 85 | } 86 | } 87 | 88 | void logic() { 89 | // Move snake 90 | for (int i = snakeLength - 1; i > 0; i--) { 91 | snakeX[i] = snakeX[i - 1]; 92 | snakeY[i] = snakeY[i - 1]; 93 | } 94 | switch (direction) { 95 | case 1: // Left 96 | snakeX[0]--; 97 | break; 98 | case 2: // Right 99 | snakeX[0]++; 100 | break; 101 | case 3: // Up 102 | snakeY[0]--; 103 | break; 104 | case 4: // Down 105 | snakeY[0]++; 106 | break; 107 | } 108 | 109 | // Check if snake eats food 110 | if (snakeX[0] == foodX && snakeY[0] == foodY) { 111 | snakeLength++; 112 | foodX = rand() % WIDTH; 113 | foodY = rand() % HEIGHT; 114 | } 115 | 116 | // Check if snake hits walls or itself 117 | if (snakeX[0] == 0 || snakeX[0] == WIDTH + 1 || snakeY[0] == 0 || snakeY[0] == HEIGHT + 1) { 118 | gameover = 1; 119 | } 120 | for (int i = 1; i < snakeLength; i++) { 121 | if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) { 122 | gameover = 1; 123 | } 124 | } 125 | } 126 | 127 | int main() { 128 | setup(); 129 | while (!gameover) { 130 | draw(); 131 | input(); 132 | logic(); 133 | usleep(100000); // Slow down the game 134 | } 135 | printf("Game over!\n"); 136 | return 0; 137 | } 138 | -------------------------------------------------------------------------------- /student-grade-system.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define MAX_STUDENTS 100 6 | #define MAX_SUBJECTS 5 7 | #define MAX_NAME_LENGTH 50 8 | 9 | struct Subject { 10 | char name[50]; 11 | int grade; 12 | }; 13 | 14 | struct Student { 15 | char name[MAX_NAME_LENGTH]; 16 | struct Subject subjects[MAX_SUBJECTS]; 17 | float gpa; 18 | }; 19 | 20 | struct Student students[MAX_STUDENTS]; 21 | int numStudents = 0; 22 | 23 | void addStudent() { 24 | if (numStudents >= MAX_STUDENTS) { 25 | printf("Maximum number of students reached!\n"); 26 | return; 27 | } 28 | 29 | struct Student newStudent; 30 | printf("Enter student name: "); 31 | scanf("%s", newStudent.name); 32 | 33 | for (int i = 0; i < MAX_SUBJECTS; i++) { 34 | printf("Enter subject name and grade (e.g., Math 90, or enter 'done' to stop): "); 35 | scanf("%s", newStudent.subjects[i].name); 36 | if (strcmp(newStudent.subjects[i].name, "done") == 0) { 37 | break; 38 | } 39 | scanf("%d", &newStudent.subjects[i].grade); 40 | } 41 | 42 | students[numStudents++] = newStudent; 43 | } 44 | 45 | void calculateGPA(struct Student *student) { 46 | float totalGrade = 0.0; 47 | for (int i = 0; i < MAX_SUBJECTS; i++) { 48 | totalGrade += student->subjects[i].grade; 49 | } 50 | student->gpa = totalGrade / MAX_SUBJECTS; 51 | } 52 | 53 | void updateGrades() { 54 | char name[MAX_NAME_LENGTH]; 55 | printf("Enter student name to update grades: "); 56 | scanf("%s", name); 57 | 58 | for (int i = 0; i < numStudents; i++) { 59 | if (strcmp(students[i].name, name) == 0) { 60 | for (int j = 0; j < MAX_SUBJECTS; j++) { 61 | printf("Enter new grade for %s (or enter 'done' to stop): ", students[i].subjects[j].name); 62 | scanf("%s", name); 63 | if (strcmp(name, "done") == 0) { 64 | break; 65 | } 66 | scanf("%d", &students[i].subjects[j].grade); 67 | } 68 | calculateGPA(&students[i]); 69 | printf("Grades updated successfully!\n"); 70 | return; 71 | } 72 | } 73 | printf("Student not found!\n"); 74 | } 75 | 76 | void generateReport() { 77 | printf("Student Name\tGPA\n"); 78 | for (int i = 0; i < numStudents; i++) { 79 | printf("%s\t%.2f\n", students[i].name, students[i].gpa); 80 | } 81 | } 82 | 83 | int main() { 84 | int choice; 85 | do { 86 | printf("\nStudent Grade Tracker Menu:\n"); 87 | printf("1. Add Student\n"); 88 | printf("2. Update Grades\n"); 89 | printf("3. Generate Report\n"); 90 | printf("4. Exit\n"); 91 | printf("Enter your choice: "); 92 | scanf("%d", &choice); 93 | 94 | switch (choice) { 95 | case 1: 96 | addStudent(); 97 | break; 98 | case 2: 99 | updateGrades(); 100 | break; 101 | case 3: 102 | generateReport(); 103 | break; 104 | case 4: 105 | printf("Exiting program.\n"); 106 | break; 107 | default: 108 | printf("Invalid choice. Please try again.\n"); 109 | } 110 | } while (choice != 4); 111 | 112 | return 0; 113 | } 114 | -------------------------------------------------------------------------------- /voting-system.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX_CANDIDATES 5 4 | #define MAX_VOTERS 50 5 | 6 | struct Candidate { 7 | char name[50]; 8 | int votes; 9 | }; 10 | 11 | int main() { 12 | struct Candidate candidates[MAX_CANDIDATES]; 13 | int numCandidates, numVoters, i, j; 14 | char voterName[50], candidateName[50]; 15 | 16 | printf("Enter the number of candidates (up to %d): ", MAX_CANDIDATES); 17 | scanf("%d", &numCandidates); 18 | 19 | for (i = 0; i < numCandidates; i++) { 20 | printf("Enter candidate %d's name: ", i + 1); 21 | scanf("%s", candidates[i].name); 22 | candidates[i].votes = 0; // Initialize votes to 0 23 | } 24 | 25 | printf("Enter the number of voters (up to %d): ", MAX_VOTERS); 26 | scanf("%d", &numVoters); 27 | 28 | for (i = 0; i < numVoters; i++) { 29 | printf("Enter voter's name: "); 30 | scanf("%s", voterName); 31 | 32 | printf("Candidates:\n"); 33 | for (j = 0; j < numCandidates; j++) { 34 | printf("%d. %s\n", j + 1, candidates[j].name); 35 | } 36 | 37 | printf("Enter the candidate number you want to vote for: "); 38 | int candidateNumber; 39 | scanf("%d", &candidateNumber); 40 | 41 | if (candidateNumber >= 1 && candidateNumber <= numCandidates) { 42 | candidates[candidateNumber - 1].votes++; 43 | printf("Thank you, %s, for voting!\n", voterName); 44 | } else { 45 | printf("Invalid candidate number!\n"); 46 | } 47 | } 48 | 49 | printf("Voting results:\n"); 50 | for (i = 0; i < numCandidates; i++) { 51 | printf("%s: %d votes\n", candidates[i].name, candidates[i].votes); 52 | } 53 | 54 | // Find the candidate with the most votes 55 | int maxVotes = 0, winnerIndex = -1; 56 | for (i = 0; i < numCandidates; i++) { 57 | if (candidates[i].votes > maxVotes) { 58 | maxVotes = candidates[i].votes; 59 | winnerIndex = i; 60 | } 61 | } 62 | 63 | if (winnerIndex != -1) { 64 | printf("The winner is: %s with %d votes!\n", candidates[winnerIndex].name, maxVotes); 65 | } else { 66 | printf("No winner!\n"); 67 | } 68 | 69 | return 0; 70 | } --------------------------------------------------------------------------------