Arithmetic operators
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 3;
int c = a + b;
int d = a - b;
int e = a * b;
cout << "Addition = " << c << endl;
cout << "Subtraction = " << d << endl;
cout << "Multiplication = " << e << endl;
return 0;
}
Division and modulus
#include <iostream>
using namespace std;
int main() {
int a = 17, b = 5;
cout << "Division = " << a / b << endl;
cout << "Modulus = " << a % b << endl;
return 0;
}
Increment and decrement
#include <iostream>
using namespace std;
int main() {
int i = 6, j = 7;
cout << "i before: " << i << endl;
cout << "Post-increment: " << i++ << endl;
cout << "After post-increment: " << i << endl;
cout << "Pre-increment: " << ++i << endl;
cout << "j before: " << j << endl;
cout << "Post-decrement: " << j-- << endl;
cout << "After post-decrement: " << j << endl;
cout << "Pre-decrement: " << --j << endl;
return 0;
}
Logical operators
#include <iostream>
using namespace std;
int main() {
bool t = true;
bool f = false;
cout << "t = " << t << endl;
cout << "f = " << f << endl;
cout << "AND = " << (t && f) << endl;
cout << "OR = " << (t || f) << endl;
cout << "NOT t = " << (!t) << endl;
cout << "NOT f = " << (!f) << endl;
cout << "XOR expression = " << ((t || f) ^ (t == f)) << endl;
return 0;
}
Assignment operators
#include <iostream>
#include <string>
using namespace std;
int main() {
int x = 8;
int y = x;
string text = "C++ Programming Language";
cout << "x = " << x << endl;
cout << "y = " << y << endl;
cout << text << endl;
int a = 5, b = 4;
a += b;
cout << "a += b: " << a << endl;
a *= b;
cout << "a *= b: " << a << endl;
a -= b;
cout << "a -= b: " << a << endl;
a %= b;
cout << "a %= b: " << a << endl;
double c = 7, d = 3;
c /= d;
cout << "c /= d: " << c << endl;
return 0;
}
Comparison operators
#include <iostream>
using namespace std;
int main() {
int x = 5, y = 3;
cout << (x == y) << endl;
cout << (x != y) << endl;
cout << (x > y) << endl;
cout << (x < y) << endl;
cout << (x >= y) << endl;
cout << (x <= y) << endl;
return 0;
}
Type conversion
#include <iostream>
using namespace std;
int main() {
double a = 5.8;
int b = int(a);
double c = static_cast<double>(b);
cout << "int(a) = " << b << endl;
cout << "double(b) = " << c << endl;
return 0;
}
Exercise 1 – sum of two input numbers
#include <iostream>
using namespace std;
int main() {
double a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Sum = " << a + b;
return 0;
}
Exercise 2 – sum, difference, product and quotient
#include <iostream>
using namespace std;
int main() {
double a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Sum = " << a + b << endl;
cout << "Difference = " << a - b << endl;
cout << "Product = " << a * b << endl;
cout << "Quotient = " << a / b << endl;
return 0;
}
Exercise 3 – area of rectangle
#include <iostream>
using namespace std;
int main() {
double length, width;
cout << "Enter length and width: ";
cin >> length >> width;
cout << "Area = " << length * width;
return 0;
}
Exercise 4 – average of three numbers
#include <iostream>
using namespace std;
int main() {
double a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
cout << "Average = " << (a + b + c) / 3;
return 0;
}
Exercise 5 – Fahrenheit to Celsius
#include <iostream>
using namespace std;
int main() {
double fahrenheit, celsius;
cout << "Enter Fahrenheit: ";
cin >> fahrenheit;
celsius = (5.0 / 9.0) * (fahrenheit - 32);
cout << "Celsius = " << celsius;
return 0;
}Ready to practice this lecture?