Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
C++ program for Complex Number Calculator
Next article icon

C++ Program to Make a Simple Calculator

Last Updated : 15 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A simple calculator is a device used to perform basic arithmetic operations such as addition, subtraction, multiplication, and division. It makes arithmetic calculations easier and faster. In this article, we will learn how to code a simple calculator using C++.

Examples

Input: Enter an operator (+, -, *, /): *
Enter two numbers: 10 5
Output: Result: 50
Explanation: Chosen operation is addition, so 10 * 5 = 50

There are two different ways to make simple calculator program in C++:

Table of Content

  • Using Switch Statement
  • Using if-else Statement

Using Switch Statement

In C++, the switch statement allows us to define different cases that will be executed based on the value of a given variable. We can use it to define four cases for addition, subtraction, multiplication, and division which will be executed based on the value that user input to select the operation type.

Code Implementation

C++
// C++ Program to make a Simple Calculator using // switch-case statements #include <bits/stdc++.h> using namespace std;  int main() {     char op;     double a, b, res;      // Read the operator     cout << "Enter an operator (+, -, *, /): ";     cin >> op;      // Read the two numbers     cout << "Enter two numbers: ";     cin >> a >> b;      // Define all four operations in the corresponding     // switch-case     switch (op) {     case '+':         res = a + b;         break;     case '-':         res = a - b;         break;     case '*':         res = a * b;         break;     case '/':         res = a / b;         break;     default:         cout << "Error! Operator is not correct";         res = -DBL_MAX;     } 	   	// Printing the result     if (res != -DBL_MAX)         cout << "Result: " << res;     return 0; } 


Output

Enter an operator (+, -, *, /): * Enter two numbers: 10 5 Result: 50

Note: The break statement in each switch case is used to come out of the switch statement when same case is matched and executed. Otherwise, all the cases after the matching case will be executed.

Using if-else Statement

In C++, the if-else if ladder can also be used to create a simple calculator. Here, we define all the cases inside the corresponding if and else-if condition block and again by matching the given user input, we will perform the corresponding arithmetic operation. But the syntax is more complex in comparison to a switch statement.

Code Implementation

C++
// C++ Program to implement a Simple Calculator // using if-else statements #include <bits/stdc++.h> using namespace std;  int main() {     char op;     double a, b, res;      // Read the operator     cout << "Enter an operator (+, -, *, /): ";     cin >> op;      // Read the two numbers     cout << "Enter two numbers: ";     cin >> a >> b;      // Perform the operation corresponding to the     //  given operator     if (op == '+')         res = a + b;     else if (op == '-')         res = a - b;     else if (op == '*')         res = a * b;     else if (op == '/')         res = a / b;     else {         cout << "Error! Operator is not correct";         res = -DBL_MAX;     }      if (res != -DBL_MAX)         cout << "Result: " << res;     return 0; } 


Output

Enter an operator (+, -, *, /): * Enter two numbers: 10 5 Result: 50

Time Complexity: O(1)
Auxiliary Space: O(1)



Next Article
C++ program for Complex Number Calculator

M

MinalJain
Improve
Article Tags :
  • Technical Scripter
  • C++ Programs
  • C++
  • CPP-Basics
  • CPP Examples
Practice Tags :
  • CPP

Similar Reads

  • Menu Driven C++ Program for a Simple Calculator
    Problem Statement: Write a menu-driven program using the Switch case to calculate the following: Addition of two numbersDifference between two numbersProduct of two numbersDivision of two numbersHCF of two numbersLCM of two numbers Examples: Input: num1 = 5, num2 = 7, choice = 1 Output: Sum is 12 In
    3 min read
  • C Program to Calculate Sum of Array Elements
    In this article, we will learn how to find the sum of elements of an array using a C program. The simplest method to calculate the sum of elements in an array is by iterating through the entire array using a loop while adding each element to the accumulated sum. [GFGTABS] C #include <stdio.h>
    2 min read
  • C++ Program To Find Simple Interest
    What is 'Simple Interest'? Simple interest is a quick method of calculating the interest charge on a loan. Simple interest is determined by multiplying the daily interest rate by the principal by the number of days that elapse between payments. Simple Interest formula: Simple interest formula is giv
    2 min read
  • C++ Program to Perform Calculations in Pure Strings
    Given a string of operations containing three operands for each operation "type of command", "first operand", and "second operand". Calculate all the commands given in this string format. In other words, you will be given a pure string that will ask you to perform an operation and you have to perfor
    5 min read
  • C++ program for Complex Number Calculator
    Pre-Requisites: Complex Numbers, Mathematics, Object-oriented Programming This is a Complex Number Calculator which performs a lot of operations which are mentioned below, to simplify our day-to-day problems in mathematics. The implementation of this calculator is done using C++ Programming Language
    15+ min read
  • C++ Program To Print Multiplication Table of a Number
    A multiplication table shows a list of multiples of a particular number, from 1 to 10. In this article, we will learn to generate and display the multiplication table of a number in C++ programming language. Recommended: Please try your approach on {IDE} first, before moving on to the solution.Algor
    3 min read
  • How to Pause a Program in C++?
    Pausing programs in C ++ is an important common task with many possible causes like debugging, waiting for user input, and providing short delays between multiple operations. In this article, we will learn how to pause a program in C++. Pause Console in a C++ ProgramWe can use the std::cin::get() me
    2 min read
  • Calculator using Classes in C++
    Implementing a calculator in C++ using the concept of the classes. Functions: Addition of two numbers.Difference between two numbers.Product of two numbers.Division of two numbers. Approach: Declare local variables a, b for two numeric values.Enter the Choice.Takes two numbers, a and b.do-while jump
    2 min read
  • C++ Program to Swap Two Numbers
    Swapping numbers is the process of interchanging their values. In this article, we will learn algorithms and code to swap two numbers in the C++ programming language. 1. Swap Numbers Using a Temporary VariableWe can swap the values of the given two numbers by using another variable to temporarily st
    4 min read
  • C++ program to implement Full Adder
    Prerequisite : Full AdderWe are given three inputs of Full Adder A, B,C-IN. The task is to implement the Full Adder circuit and Print output i.e. sum and C-Out of three inputs. Introduction : A Full Adder is a combinational circuit that performs an addition operation on three 1-bit binary numbers. T
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences