Arithmetic operators

In C++ there are the usual arithmetic operators as in most other programming languages, namely addition, subtraction, multiplication, division and the remainder operator (modulo). In addition, there are the one-digit operators for positive and negative sign.

OperatorDescription
+Addition / sign
Subtraction / sign
*Multiplication
/Division
%Rest (modulo)

Code Explanation

LineDescription
2Import the <iomanip> header to use parametric manipulators
6Initializes the integer variables a with value 4 and b with value 6
7Creates a variable c of type long and assigns it the value 7
8Creates a variable z of type double and initializes it the value 0
9 – 10Outputs the starting values
12 – 13Divides variable a with variable b and sets the data type to double
16Outputs the result of the division. The setprecision() function is used to specify the decimal places
18 – 22Checks whether variable a is even or odd. If you divide a by 2 and the result has no remainder, it is an even number.
#include <iostream>
#include <iomanip>	// Library for specifying the decimal places
using namespace std;

int main() {
	int a = 4, b = 6;
	long c = 7;
	double z = 0;
	cout << "Starting values" << endl;
	cout << "a = " << a << "\nb = " << b << "\nc = " << c << endl;

	cout << "Calculations" << endl;
	z = (double)a / (double)b;
	// setprecision() = Funktion from library <iomanip>,
	// for specifying the decimal places
	cout << "z = a / b = " << setprecision(2) << z << endl;

	if (a % 2 == 0) {
		cout << "Number " << a << " = even" << endl;
	} else {
		cout << "Number " << a << " = odd" << endl;
	}
}
Output
Starting values
a = 4
b = 6
c = 7
Calculations
z = a / b = 0.67
Number 4 = even