Chapter 17: Mini Projects & Practice
Now that you’ve learned all the important concepts of C programming, it’s time to put everything together and build some real, useful mini projects! These projects will help you:
- Combine all concepts (variables, loops, functions, arrays, structures, file handling, pointers, etc.)
- Write clean, organized code
- Debug and improve your programs
- Feel confident that you can build actual software
We will build 5 classic mini projects – each with:
- Problem description
- Planning (what features, what data structures)
- Complete working code
- Explanation of important parts
- Ideas to improve/extend the project
Let’s start!
Project 1: Simple Calculator (Using Functions & Switch)
Features:
- Add, Subtract, Multiply, Divide
- Take two numbers and operation from user
- Repeat until user wants to exit
|
0 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 |
#include <stdio.h> // Function prototypes float add(float a, float b); float subtract(float a, float b); float multiply(float a, float b); float divide(float a, float b); int main() { float num1, num2, result; char operation; char choice; do { printf("\n=== Simple Calculator ===\n"); printf("Enter first number: "); scanf("%f", &num1); printf("Enter operator (+, -, *, /): "); scanf(" %c", &operation); // space before %c to eat newline printf("Enter second number: "); scanf("%f", &num2); switch (operation) { case '+': result = add(num1, num2); printf("%.2f + %.2f = %.2f\n", num1, num2, result); break; case '-': result = subtract(num1, num2); printf("%.2f - %.2f = %.2f\n", num1, num2, result); break; case '*': result = multiply(num1, num2); printf("%.2f * %.2f = %.2f\n", num1, num2, result); break; case '/': if (num2 != 0) { result = divide(num1, num2); printf("%.2f / %.2f = %.2f\n", num1, num2, result); } else { printf("Error: Division by zero!\n"); } break; default: printf("Invalid operator!\n"); } printf("\nDo you want to continue? (y/n): "); scanf(" %c", &choice); } while (choice == 'y' || choice == 'Y'); printf("Thank you for using the calculator!\n"); return 0; } // Function definitions float add(float a, float b) { return a + b; } float subtract(float a, float b) { return a - b; } float multiply(float a, float b) { return a * b; } float divide(float a, float b) { return a / b; } |
Improvements you can do:
- Add more operations (power, square root, modulus)
- Add memory functions (M+, M-, MR, MC)
- Use command-line arguments for numbers
Project 2: Student Record System (Using Structures & Arrays)
Features:
- Add student
- View all students
- Search student by roll number
- Update marks
- Delete student
|
0 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 |
#include <stdio.h> #include <string.h> #define MAX_STUDENTS 100 typedef struct { char name[50]; int roll; float marks; } Student; Student students[MAX_STUDENTS]; int studentCount = 0; // Function prototypes void addStudent(); void viewAll(); void searchStudent(); void updateMarks(); void deleteStudent(); int main() { int choice; do { printf("\n=== Student Record System ===\n"); printf("1. Add Student\n"); printf("2. View All Students\n"); printf("3. Search Student\n"); printf("4. Update Marks\n"); printf("5. Delete Student\n"); printf("6. Exit\n"); printf("Enter choice: "); scanf("%d", &choice); switch (choice) { case 1: addStudent(); break; case 2: viewAll(); break; case 3: searchStudent(); break; case 4: updateMarks(); break; case 5: deleteStudent(); break; case 6: printf("Goodbye!\n"); break; default: printf("Invalid choice!\n"); } } while (choice != 6); return 0; } void addStudent() { if (studentCount >= MAX_STUDENTS) { printf("Maximum students reached!\n"); return; } printf("Enter name: "); scanf("%s", students[studentCount].name); printf("Enter roll number: "); scanf("%d", &students[studentCount].roll); printf("Enter marks: "); scanf("%f", &students[studentCount].marks); studentCount++; printf("Student added successfully!\n"); } void viewAll() { if (studentCount == 0) { printf("No students found!\n"); return; } printf("\n--- All Students ---\n"); for (int i = 0; i < studentCount; i++) { printf("Roll: %d | Name: %s | Marks: %.2f\n", students[i].roll, students[i].name, students[i].marks); } } // You can implement searchStudent(), updateMarks(), deleteStudent() similarly // (search by roll, update marks, shift array when deleting) |
Improvements:
- Add file handling (save/load students from file)
- Add sort by marks/roll
- Add average class marks calculation
Project 3: Bank Management System (Using Structures & File Handling)
Features:
- Create account
- Deposit
- Withdraw
- Check balance
- Save/load accounts from file
(We’ll make a simple version – you can expand it.)
|
0 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 |
#include <stdio.h> #include <string.h> typedef struct { int accountNumber; char name[50]; float balance; } Account; #define MAX_ACCOUNTS 100 Account accounts[MAX_ACCOUNTS]; int accountCount = 0; void loadAccounts(); void saveAccounts(); void createAccount(); void deposit(); void withdraw(); void checkBalance(); int main() { loadAccounts(); // Load from file at start int choice; do { printf("\n=== Bank Management System ===\n"); printf("1. Create Account\n"); printf("2. Deposit\n"); printf("3. Withdraw\n"); printf("4. Check Balance\n"); printf("5. Exit\n"); printf("Enter choice: "); scanf("%d", &choice); switch (choice) { case 1: createAccount(); break; case 2: deposit(); break; case 3: withdraw(); break; case 4: checkBalance(); break; case 5: saveAccounts(); printf("Goodbye!\n"); break; default: printf("Invalid choice!\n"); } } while (choice != 5); return 0; } void loadAccounts() { FILE *fp = fopen("accounts.dat", "rb"); if (fp == NULL) return; fread(&accountCount, sizeof(int), 1, fp); fread(accounts, sizeof(Account), accountCount, fp); fclose(fp); } void saveAccounts() { FILE *fp = fopen("accounts.dat", "wb"); if (fp == NULL) { printf("Error saving accounts!\n"); return; } fwrite(&accountCount, sizeof(int), 1, fp); fwrite(accounts, sizeof(Account), accountCount, fp); fclose(fp); printf("Accounts saved successfully!\n"); } void createAccount() { if (accountCount >= MAX_ACCOUNTS) { printf("Maximum accounts reached!\n"); return; } printf("Enter account number: "); scanf("%d", &accounts[accountCount].accountNumber); printf("Enter name: "); scanf("%s", accounts[accountCount].name); printf("Enter initial balance: "); scanf("%f", &accounts[accountCount].balance); accountCount++; printf("Account created successfully!\n"); } // Implement deposit, withdraw, checkBalance similarly... |
Project 4: Tic-Tac-Toe Game
Features:
- 3×3 board
- Two players (X and O)
- Check win/draw
- Play again option
(Full code is a bit long – I’ll give the main structure and key functions)
|
0 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 |
#include <stdio.h> char board[3][3] = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}}; char currentPlayer = 'X'; void printBoard(); int checkWin(); int checkDraw(); void makeMove(); int main() { int gameOver = 0; printf("Welcome to Tic-Tac-Toe!\n"); while (!gameOver) { printBoard(); makeMove(); if (checkWin()) { printBoard(); printf("Player %c wins!\n", currentPlayer); gameOver = 1; } else if (checkDraw()) { printBoard(); printf("It's a draw!\n"); gameOver = 1; } currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; } return 0; } void printBoard() { printf("\n"); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { printf(" %c ", board[i][j]); if (j < 2) printf("|"); } printf("\n"); if (i < 2) printf("---+---+---\n"); } printf("\n"); } // Implement checkWin(), checkDraw(), makeMove()... |
Project 5: File-Based Phone Book
Features:
- Add contact
- View all contacts
- Search by name
- Delete contact
- Save/load from file
(You can build this using structures + file handling – similar to bank system)
