๐Ÿงฎ 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

  1. Open any C compiler (GCC, CodeBlocks, Turbo C)

  2. Copy and paste the code

  3. Compile the program

  4. Run the executable

  5. 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

Popular posts from this blog

๐ŸŒŸ The Honest Journey of a Student: Learning, Failing, and Growing

“C Programming for Beginners: Master Variables, Data Types, and Memory (Bits Explained)”