Lab 2
Basic Arithmetic Operations
Objective:
Implement arithmetic operations using C++.
Practice taking user input and performing calculations.
Topics Covered:
1. Arithmetic Operators:
o Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus (%).
2. Taking User Input:
o Using cin for integer input.
3. Displaying Output:
o Using cout to display results.
4. Handling Division by Zero:
o Implementing conditions to prevent division by zero.
1. Addition of Two Numbers
#include <iostream>
using namespace std;
int main() {
int a, b, sum;
cout << "Enter two numbers: ";
cin >> a >> b;
sum = a + b;
cout << "Sum: " << sum << endl;
return 0;
}
2. Subtraction of Two Numbers
#include <iostream>
using namespace std;
int main() {
int a, b, result;
cout << "Enter two numbers: ";
cin >> a >> b;
result = a - b;
cout << "Subtraction result: " << result << endl;
return 0;
}
3. Multiplication of Two Numbers
#include <iostream>
using namespace std;
int main() {
int a, b, product;
cout << "Enter two numbers: ";
cin >> a >> b;
product = a * b;
cout << "Multiplication result: " << product << endl;
return 0;
}
4. Division of Two Numbers (with Zero Check)
#include <iostream>
using namespace std;
int main() {
double a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
if (b != 0) {
cout << "Division result: " << a / b << endl;
} else {
cout << "Error: Division by zero is not allowed!" << endl;
}
return 0;
}
Tasks:
1. Modify the addition program to accept three numbers.
2. Create a program that calculates the remainder of two numbers using the modulus
operator %.
3. Write a program to find the average of three numbers.