0% found this document useful (0 votes)
10 views3 pages

Operators

Operators in C++

Uploaded by

landeparth93
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Operators

Operators in C++

Uploaded by

landeparth93
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Exercise 1: Arithmetic Operators

Write a program that takes two integers as input and performs the following operations:

 Addition
 Subtraction
 Multiplication
 Division
 Modulus

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
int a, b;
cout << "Enter two integers: ";
cin >> a >> b;

cout << "Addition: " << a + b << endl;


cout << "Subtraction: " << a - b << endl;
cout << "Multiplication: " << a * b << endl;
cout << "Division: " << a / b << endl; // Handle division by zero
cout << "Modulus: " << a % b << endl; // Handle modulus by zero

return 0;
}

Exercise 2: Relational Operators

Create a program that compares two floating-point numbers and prints whether the first is
greater than, less than, or equal to the second.

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
float num1, num2;
cout << "Enter two floating-point numbers: ";
cin >> num1 >> num2;

if (num1 > num2) {


cout << num1 << " is greater than " << num2 << endl;
} else if (num1 < num2) {
cout << num1 << " is less than " << num2 << endl;
} else {
cout << num1 << " is equal to " << num2 << endl;
}

return 0;
}

Exercise 3: Logical Operators


Write a program that checks if a number is within a certain range (e.g., 1 to 100). Use logical
operators to determine if the input number falls within this range.

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
int number;
cout << "Enter a number: ";
cin >> number;

if (number >= 1 && number <= 100) {


cout << number << " is within the range of 1 to 100." << endl;
} else {
cout << number << " is out of range." << endl;
}

return 0;
}

Exercise 4: Assignment Operators

Create a simple calculator program that performs addition, subtraction, multiplication, and
division. Use assignment operators to store the results.

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
int a, b;
cout << "Enter two integers: ";
cin >> a >> b;

int result = 0;

result += a; // Initialize result with a


result += b; // Addition
cout << "Sum: " << result << endl;

result -= b; // Subtract b
cout << "Difference: " << result << endl;

result *= a; // Multiplication
cout << "Product: " << result << endl;

if (b != 0) {
result /= b; // Division
cout << "Quotient: " << result << endl;
} else {
cout << "Cannot divide by zero!" << endl;
}

return 0;
}
Exercise 5: Increment and Decrement Operators

Write a program that demonstrates the use of increment (++) and decrement (--) operators.

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
int count = 5;

cout << "Initial count: " << count << endl;

// Pre-increment
cout << "Pre-increment: " << ++count << endl;

// Post-increment
cout << "Post-increment: " << count++ << endl;
cout << "Count after post-increment: " << count << endl;

// Pre-decrement
cout << "Pre-decrement: " << --count << endl;

// Post-decrement
cout << "Post-decrement: " << count-- << endl;
cout << "Count after post-decrement: " << count << endl;

return 0;
}

You might also like