🎓 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
Calculators are fundamental tools that can be found virtually everywhere, from our smartphones to standalone devices. Have you ever wondered how to build a simple one using C++? This blog post will guide you through the process of creating a basic calculator that can add, subtract, multiply, and divide.
2. Program Overview
Our calculator will:
1. Prompt the user to input two numbers.
2. Ask the user to select an operation (add, subtract, multiply, or divide).
3. Perform the selected operation and display the result.
3. Code Program
#include<iostream>
using namespace std;
int main() {
// Declare variables for the two numbers, the result of the operation, and the operation itself
double num1, num2, result;
char operation;
// Prompt user to input the first number
cout << "Enter first number: ";
cin >> num1;
// Prompt user to input the second number
cout << "Enter second number: ";
cin >> num2;
// Prompt user to specify the desired arithmetic operation
cout << "Enter operation (+, -, *, /): ";
cin >> operation;
// Use a switch-case construct to determine the operation and calculate the result
switch(operation) {
case '+':
// Addition
result = num1 + num2;
break;
case '-':
// Subtraction
result = num1 - num2;
break;
case '*':
// Multiplication
result = num1 * num2;
break;
case '/':
// Division
if(num2 != 0) { // Ensure denominator is not zero
result = num1 / num2;
} else {
// Division by zero is undefined, hence display an error message
cout << "Division by zero is not allowed!";
return 1; // Return with an error code
}
break;
default:
// Inform the user if they entered an invalid operation
cout << "Invalid operation!";
return 1; // Return with an error code
}
// Display the result of the operation
cout << "Result: " << result << endl;
return 0; // Successful completion of program
}
Output:
Enter first number: 5 Enter second number: 3 Enter operation (+, -, *, /): + Result: 8
4. Step By Step Explanation
1. We use cin to get two numbers and an operation symbol from the user.
2. Using a switch statement, we branch to the appropriate arithmetic operation based on the symbol entered.
3. If the division operation is chosen and the second number is 0, we display an error message as dividing by zero is undefined in mathematics.
4. If a valid operation is chosen, the calculator displays the result. If not, an error message is shown.
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