Write a C++ Program to Perform Arithmetic Operations on Two Numbers
Performing arithmetic operations is one of the first programs every beginner learns in C++. It helps understand user input, conditional statements, and basic operators. In this tutorial by cslearning.org, we will learn how to write a simple and student-friendly C++ program to accept two integers and an arithmetic operator (+, -, *, /) from the user, perform the operation, and display the result.
This guide includes introduction, logic, simple C++ source code, sample output, and viva questions with answers.
1. Introduction of Arithmetic Operations
Arithmetic operations are basic mathematical operations such as:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
In C++, we can perform these operations on numbers using operators. The program will allow the user to choose the operation by entering the operator symbol.
Example:
If user enters 10, 5, and + → Result = 15
If user enters 12, 3, and / → Result = 4
2. Logic of the Program
To perform arithmetic operations on two numbers:
- Start the program.
- Declare variables to store two integers and the operator.
- Take input of two numbers and the operator from the user.
- Use conditional statements (
if-elseorswitch) to check the operator. - Perform the corresponding arithmetic operation.
- Display the result.
Example:
If user enters 8, 4, and *
8 * 4 = 32 → Display 32
3. C++ Source Code (Easy Student Friendly Code)
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
char op;
float result;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> op;
if(op == '+')
{
result = num1 + num2;
}
else if(op == '-')
{
result = num1 - num2;
}
else if(op == '*')
{
result = num1 * num2;
}
else if(op == '/')
{
if(num2 != 0)
result = (float)num1 / num2;
else
{
cout << "Error! Division by zero.";
return 0;
}
}
else
{
cout << "Invalid operator!";
return 0;
}
cout << "Result: " << result;
return 0;
}
Explanation:
char opstores the operator entered by the user.if-elsestatements check which operation to perform.- Typecasting
(float)is used for division to get decimal results. - Division by zero is handled to avoid runtime errors.
4. Sample Output
Example 1:
Enter first number: 10
Enter second number: 5
Enter an operator (+, -, *, /): +
Result: 15
Example 2:
Enter first number: 12
Enter second number: 4
Enter an operator (+, -, *, /): /
Result: 3
Example 3:
Enter first number: 7
Enter second number: 0
Enter an operator (+, -, *, /): /
Error! Division by zero.
5. Viva Questions with Answers
Q1: Which operators are used for arithmetic operations in C++?
Answer: +, -, *, / are used.
Q2: Why do we typecast to float for division?
Answer: To get decimal results instead of integer division.
Q3: Which conditional statement is used in this program?
Answer: if-else statements are used to check the operator.
Q4: How is division by zero handled?
Answer: The program checks if the second number is zero before performing division.
Q5: Can we also use a switch statement for this program?
Answer: Yes, switch can be used to check the operator and perform the operation.
This program helps beginners understand user input, conditional statements, arithmetic operations, and typecasting in C++. Practice more C++ programs on cslearning.org to strengthen your programming fundamentals.
