🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
This blog post will guide you through creating a basic calculator in C++. The calculator will be capable of performing addition, subtraction, multiplication, and division.
2. Program Steps
1. Display a menu of operations to the user.
2. Take the user's choice of operation.
3. Ask the user for two numbers.
4. Perform the chosen operation on the numbers.
5. Display the result.
6. Loop back to the menu unless the user chooses to exit.
3. Code Program
#include <iostream>
using namespace std;
void displayMenu() {
cout << "Simple Calculator\n";
cout << "1. Add\n";
cout << "2. Subtract\n";
cout << "3. Multiply\n";
cout << "4. Divide\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
}
double add(double a, double b) {
return a + b;
}
double subtract(double a, double b) {
return a - b;
}
double multiply(double a, double b) {
return a * b;
}
double divide(double a, double b) {
if (b == 0) {
cout << "Error: Division by zero!" << endl;
return 0;
}
return a / b;
}
int main() {
double num1, num2;
int choice;
do {
displayMenu();
cin >> choice;
if (choice >= 1 && choice <= 4) {
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
}
switch (choice) {
case 1:
cout << "Result: " << add(num1, num2) << endl;
break;
case 2:
cout << "Result: " << subtract(num1, num2) << endl;
break;
case 3:
cout << "Result: " << multiply(num1, num2) << endl;
break;
case 4:
cout << "Result: " << divide(num1, num2) << endl;
break;
case 5:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid choice!" << endl;
}
} while (choice != 5);
return 0;
}
Output:
Simple Calculator 1. Add 2. Subtract 3. Multiply 4. Divide 5. Exit Enter your choice: 1 Enter first number: 10 Enter second number: 20 Result: 30 Simple Calculator 1. Add 2. Subtract 3. Multiply 4. Divide 5. Exit Enter your choice: 2 Enter first number: 20 Enter second number: 10 Result: 10 Simple Calculator 1. Add 2. Subtract 3. Multiply 4. Divide 5. Exit Enter your choice: 3 Enter first number: 5 Enter second number: 2 Result: 10 Simple Calculator 1. Add 2. Subtract 3. Multiply 4. Divide 5. Exit Enter your choice: 4 Enter first number: 10 Enter second number: 3 Result: 3.33333 Simple Calculator 1. Add 2. Subtract 3. Multiply 4. Divide 5. Exit Enter your choice: 5 Exiting program.
Explanation:
1. The program starts by displaying a menu of operations to the user.
2. It reads the user's choice and, based on the selection, prompts the user to enter two numbers.
3. Depending on the operation chosen, the program calls the corresponding function (add, subtract, multiply, divide) to perform the calculation.
4. The result of the operation is then displayed to the user.
5. If the user chooses to divide by zero, an error message is displayed.
6. The program uses a loop to return to the menu after displaying the result, allowing for multiple calculations until the user decides to exit by selecting option 5.
This simple C++ calculator demonstrates basic programming concepts such as functions, loops, and conditional statements.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business

Comments
Post a Comment
Leave Comment