๐งฎ Calculator Program in C (Menu-Driven CLI Project) – Complete Guide
๐ท Introduction
In this project, we will build a Calculator Program in C using a menu-driven approach. This program allows users to perform basic arithmetic operations like addition, subtraction, multiplication, and division.
๐ This is a beginner-friendly project that helps you understand how to apply C concepts in real programs.
๐ท ๐ Project Preview
Menu-driven interface
Performs basic calculations
Easy to use
Runs continuously until user exits
๐ท ๐ Project Flow
User
↓
Menu
↓
Choose Operation
↓
Input Numbers
↓
Perform Calculation
↓
Show Result
๐ท ✨ Features
➕ Addition
➖ Subtraction
✖️ Multiplication
➗ Division
๐ Repeat operations using loop
❌ Exit option
๐ท ๐ง Concepts Used
Functions
Switch-case
Loops (while)
Basic Input/Output
๐ท ๐ Related Topics (Learn More)
Before trying this project, you should know:
Functions in C
Switch Case in C
Loops in C
These concepts are used to build the logic of this calculator.
๐ท ๐ก Real-World Use
Used in basic calculation tools and applications
Helps understand logic behind real calculators
Used in embedded systems for simple computations
Forms the base for advanced scientific calculators
Improves problem-solving and logical thinking
๐ท ๐ป Program Code (With Comments)
#include <stdio.h> // Header file for input/output functions like printf, scanf
// Function declarations
void add(); // Declaration of add function
void subtract(); // Declaration of subtract function
void multiply(); // Declaration of multiply function
void divide(); // Declaration of divide function
int main() {
int choice; // Variable to store user choice
while(1) { // Infinite loop to keep program running until user exits
printf("\n===== Calculator Menu =====\n"); // Display menu title
printf("1. Addition\n"); // Option 1
printf("2. Subtraction\n"); // Option 2
printf("3. Multiplication\n"); // Option 3
printf("4. Division\n"); // Option 4
printf("5. Exit\n"); // Option 5
printf("Enter your choice: "); // Ask user for choice
scanf("%d", &choice); // Take input from user
switch(choice) { // Switch case based on user choice
case 1: add(); break; // Call add function
case 2: subtract(); break; // Call subtract function
case 3: multiply(); break; // Call multiply function
case 4: divide(); break; // Call divide function
case 5:
printf("Exiting...\n"); // Exit message
return 0; // End program
default:
printf("Invalid choice! Try again.\n"); // Handle wrong input
}
}
return 0; // Return statement (though loop never reaches here)
}
// Function for addition
void add() {
float a, b; // Variables to store numbers
printf("Enter two numbers: "); // Ask user for input
scanf("%f %f", &a, &b); // Read two float numbers
printf("Result = %.2f\n", a + b); // Display sum with 2 decimal places
}
// Function for subtraction
void subtract() {
float a, b; // Variables to store numbers
printf("Enter two numbers: "); // Ask user for input
scanf("%f %f", &a, &b); // Read two float numbers
printf("Result = %.2f\n", a - b); // Display difference
}
// Function for multiplication
void multiply() {
float a, b; // Variables to store numbers
printf("Enter two numbers: "); // Ask user for input
scanf("%f %f", &a, &b); // Read two float numbers
printf("Result = %.2f\n", a * b); // Display product
}
// Function for division
void divide() {
float a, b; // Variables to store numbers
printf("Enter two numbers: "); // Ask user for input
scanf("%f %f", &a, &b); // Read two float numbers
if(b == 0) { // Check if denominator is zero
printf("Error! Division by zero is not allowed.\n"); // Error message
} else {
printf("Result = %.2f\n", a / b); // Display division result
}
}
๐ท ▶️ How to Run the Program
Open any C compiler (GCC, CodeBlocks, Turbo C)
Copy and paste the code
Compile the program
Run the executable
Enter your choice from the menu
๐ท ๐ฅ Sample Output
===== Calculator Menu =====
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 1
Enter two numbers: 10 5
Result = 15.00
๐ท ๐ Explanation
The program uses a while loop to run continuously
A menu-driven system allows user interaction
Switch-case is used to select operations
Separate functions are used for each calculation
Division includes a check to avoid division by zero
๐ท ๐ง Deep Explanation
The calculator works by taking user input through a menu system. Based on the selected option, the program calls the respective function.
Each function performs a specific arithmetic operation and returns the result. The loop ensures the program keeps running until the user selects the exit option.
The division function includes an important condition to prevent division by zero, which is a common runtime error in C programs.
๐ท ๐ Future Improvements
You can improve this project by adding:
Scientific operations (power, square root)
History of calculations
GUI-based calculator
Keyboard shortcuts
๐ท ⭐ Important Note
This project is very important for beginners and is commonly asked in exams and interviews. It helps in understanding the core logic of programming.
๐ท ๐ Conclusion
This calculator project is a simple yet powerful way to understand how C programming works in real applications. It strengthens your logic-building skills and prepares you for more advanced projects.
⭐ Tip: Try adding new features and make your own advanced calculator!
๐ Keep Learning. Keep Coding. Keep Growing.
✨ Written by Krishna Popat
๐ฑ Founder, Learning Growth Hub
Comments
Post a Comment