0% found this document useful (0 votes)
2 views

Lab 2

The document outlines a lab exercise focused on basic arithmetic operations in C++. It covers arithmetic operators, user input, output display, and handling division by zero, with example C++ code for addition, subtraction, multiplication, and division. Additionally, it includes tasks for students to modify existing programs and create new ones using the modulus operator and to calculate averages.

Uploaded by

mufassir abbasi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab 2

The document outlines a lab exercise focused on basic arithmetic operations in C++. It covers arithmetic operators, user input, output display, and handling division by zero, with example C++ code for addition, subtraction, multiplication, and division. Additionally, it includes tasks for students to modify existing programs and create new ones using the modulus operator and to calculate averages.

Uploaded by

mufassir abbasi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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.

You might also like