QUESTION #018

Simple Calculator with Operator Detection

Easy

📋 Problem Statement

Get three inputs in the format: integer, character, integer. The inputs are all separated by spaces. Perform the operation with the integer values based on the character (+, -, *, /). Display the calculated value as the output.

Example: 23 + 45 o/p : 68

Operator Operation Example
+ Addition 17 + 15 = 32
- Subtraction 20 - 8 = 12
* Multiplication 5 * 6 = 30
/ Division (Integer) 11 / 7 = 1
% Modulo (Remainder) 11 % 7 = 4

📥 Input Format

Accept an Integer, Character, Integer as input (space-separated)

📤 Output Format

Print the output as integer

💡 Examples

Example 1:
Input: 17 + 15
Output: 32
Explanation:
Operator is '+', so perform addition: 17 + 15 = 32
Example 2:
Input: 11 / 7
Output: 1
Explanation:
Operator is '/', so perform integer division: 11 / 7 = 1 (quotient only, no decimals)
Example 3:
Input: 11 % 7
Output: 4
Explanation:
Operator is '%', so find remainder: 11 % 7 = 4

⚠️ Constraints

-10^9 <= INPUT <= 10^9

✅ Solution

#include <iostream>

using namespace std;

int main() {
    long long a, b, ans;
    char ch;
    
    cin >> a >> ch >> b;
    
    if (ch == '/')
        ans = a / b;
    else if (ch == '+')
        ans = a + b;
    else if (ch == '-')
        ans = a - b;
    else if (ch == '*')
        ans = a * b;
    else if (ch == '%')
        ans = a % b;
    
    cout << ans;
    
    return 0;
}

🔑 Key Concepts

  • Character Comparison: Using == to compare characters for operator detection
  • Multiple Input Types: Reading integer, character, and integer in sequence
  • Else-if Ladder: Testing multiple conditions for different operators
  • Long Long Data Type: Handling large numbers up to 10^9
  • Integer Division: Division of integers gives quotient, not decimal result
  • Modulo Operator: Finding remainder using % operator
  • Calculator Logic: Foundation for building more complex calculators

⚠️ Common Mistakes to Avoid

  • Single vs Double Quotes: Use '+' (single quotes) for characters, not "+" (double quotes for strings)
  • Uninitialized Variable: ans should be assigned in all cases, or use else for invalid operator
  • Division by Zero: No check for b == 0 when dividing - could cause runtime error
  • Input Order: Must read in correct order: number, operator, number (with spaces)
  • Data Type Mismatch: Using int instead of long long may cause overflow for large numbers
  • Wrong Operator Order: Checking division first is fine, but be consistent with logic

💡 Alternative: Switch Statement

This problem is perfect for demonstrating switch statement, which is cleaner for character-based decisions:

switch (ch) {
    case '+':
        ans = a + b;
        break;
    case '-':
        ans = a - b;
        break;
    case '*':
        ans = a * b;
        break;
    case '/':
        ans = a / b;
        break;
    case '%':
        ans = a % b;
        break;
    default:
        cout << "Invalid operator";
}
  • Advantages: More readable, faster for many cases, clearly shows discrete choices
  • When to use: Checking equality against multiple constant values (like operators)
  • Don't forget: break statements prevent fall-through to next case

🎯 What You'll Learn

  • How to compare characters using equality operator
  • Reading mixed data types in a single input line
  • Implementing basic calculator logic with conditionals
  • Understanding integer division vs modulo operation
  • When to use else-if vs switch for character matching
  • Handling multiple arithmetic operations dynamically
  • Real-world application: Building calculator programs

🚀 Enhancement Ideas

  • Input Validation: Check if operator is valid before performing calculation
  • Division by Zero: Add check: if (b == 0 && (ch == '/' || ch == '%'))
  • Floating Point: Use double for decimal division results
  • Multiple Operations: Allow chaining operations like "5 + 3 * 2"
  • Error Messages: Print helpful error for invalid operators or division by zero