Skip to main content

Featured

C Program to Solve Two Sum Using Brute Force (With Algorithm & Output)

 Introduction The Two Sum problem is a popular coding interview question where we must find two indices of an array whose values add up to a given target. This program demonstrates a simple brute-force solution in C using nested loops and dynamic memory allocation. Problem Statement Given an integer array and a target value, return the indices of the two numbers such that they add up to the target. Each input has exactly one solution, and the same element cannot be used twice. The result should return the indices, not the values. If no solution exists, return NULL.  Algorithm / Logic Explanation Start the program. Traverse the array using a loop from index 0 to numsSize - 1 . Inside this loop, use another loop starting from i + 1 to numsSize - 1 . For every pair (i, j) , check if nums[i] + nums[j] == target . If condition becomes true: Allocate memory for 2 integers using malloc() . Store indices i and j . Set returnSize = 2 . Return the result poi...

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

๐ŸŒ™