Skip to main content

Featured

C Program to Check Prime Number Using Efficient Logic

  Introduction A prime number is a number that has exactly two distinct positive divisors: 1 and itself. In this program, we check whether a given number is prime or not using a simple and efficient logic. This type of program is commonly used in mathematics, competitive programming, and basic algorithm learning for beginners in C programming. Problem Statement The task is to write a C program that determines whether a given integer is a prime number or not. The program takes a single integer input from the user and analyzes its divisibility. If the number has no divisors other than 1 and itself, it should be identified as a prime number; otherwise, it is not prime. This problem is important in number theory and has practical relevance in areas such as cryptography, data validation, and algorithm design.  Algorithm / Logic Explanation To check whether a number is prime, we need to verify that it is not divisible by any number other than 1 and itself. The algorithm follows a si...

Menu Driven Calculator in C++ Using Switch Case (Beginner Project)

 

Introduction

A menu driven calculator is a beginner-friendly C++ project that helps understand conditional statements, loops, and user input handling. This project allows users to perform basic arithmetic operations like addition, subtraction, multiplication, and division using a simple text-based menu. It demonstrates how real-world programs interact with users repeatedly until an exit condition is met.








Problem Statement
 The objective of this project is to design a simple console-based calculator in C++ that performs basic arithmetic operations. The program should display a menu, accept the user’s choice, take two numbers as input, and display the result accordingly. It must repeatedly execute until the user selects the exit option and should also handle invalid choices and division by zero.

Algorithm / Logic Explanation

  1. Start the program.

  2. Declare variables for choice and two numbers.

  3. Display a menu with arithmetic operations and exit option.

  4. Ask the user to enter a choice.

  5. If the choice is between 1 and 4, ask the user to enter two numbers.

  6. Use a switch statement to perform the selected operation.

  7. For division, check if the second number is zero to avoid errors.

  8. Display the result of the operation.

  9. If the user selects Exit, terminate the program.

  10. Repeat the process using a do-while loop until Exit is chosen.















C++ Source Code

#include<iostream>
using namespace std;

int main()
{
    int choice;
    double a, b;

    do
    {
        cout << "======Basic Calculator=======" << endl;
        cout << "1).Addition:" << endl;
        cout << "2).Subtraction:" << endl;
        cout << "3).Multiplication:" << endl;
        cout << "4).Division:" << endl;
        cout << "5).Exit:" << endl;
        cout << "============================" << endl;
        cout << "Enter your choice:" << endl;
        cin >> choice;

        if (choice >= 1 && choice <= 4)
        {
            cout << "Enter first Number:" << endl;
            cin >> a;
            cout << "Enter Second Number:" << endl;
            cin >> b;
        }

        switch (choice)
        {
        case 1:
            cout << "Result:" << (a + b) << endl;
            break;
        case 2:
            cout << "Result:" << (a - b) << endl;
            break;
        case 3:
            cout << "Result:" << (a * b) << endl;
            break;
        case 4:
            if (b == 0)
                cout << "Error! Division by zero" << endl;
            else
                cout << "Result:" << (a / b) << endl;
            break;
        case 5:
            cout << "Exit Successful. Thank you!" << endl;
            break;
        default:
            cout << "!invalid Choice Select Between (1 to 5)" << endl;
        }

    } while (choice != 5);

    return 0;
}
 
switch-case and loop




Sample Input / Output 

 The user selects an operation from the menu, enters two numbers, and the program displays the calculated result. The process continues until the user chooses the exit option.

Explanation of Code

The program begins by including the <iostream> header file to handle input and output operations. Inside the main() function, variables are declared to store the user’s choice and the two numbers required for calculation. A do-while loop is used so that the menu is displayed at least once and continues to appear until the user selects the exit option.

The menu is printed using cout, and the user’s choice is read using cin. If the choice corresponds to an arithmetic operation (1 to 4), the program asks the user to input two numbers. The switch statement then checks the selected option and performs the corresponding operation.

For addition, subtraction, and multiplication, the operations are directly performed. In the case of division, an if condition checks whether the second number is zero to prevent division by zero, which would cause a runtime error. If the choice is invalid, a default message is displayed. When the user selects option 5, the program prints an exit message and terminates gracefully.


Edge Cases / Notes

  • Division by zero is handled safely.

  • Invalid menu choices are detected and reported.

  • The program assumes numeric input; non-numeric input may cause unexpected behavior.

  • Using double allows decimal calculations.

Conclusion

This menu driven calculator project is an excellent way to practice basic C++ concepts such as loops, switch-case statements, and input handling. It forms a strong foundation for developing more advanced console-based applications in the future.

📢 Follow Us for More C & C++ Projects

▶ YouTube 📘 Facebook ✈ Telegram

Join our community for daily programming tips, MCQs, and projects.

Comments

Popular Posts

🌙