We discussed many things till now. We discussed printing the variables, primitive types, user input and some conditional statements. What we should discuss right now is the basic arithmetic.
It is very simple to do primitive calculations with primitive types. We will only work with integers here. But it will be applicable to all other primitive types (of course, ‘char’ is one of them).
Addition:
int a = 10;
int b = a + 5;
cout << b << endl; //Prints 15
int c = 12;
c = c + 5;
cout << c << endl; //Prints 17
int d = a + c;
cout << d << endl; //Prints 27
Subtraction:
int a = 10;
int b = a - 5;
cout << b << endl; //Prints 5
int c = 12;
c = c - 5;
cout << c << endl; //Prints 7
int d = a - c;
cout << d << endl; //Prints 3
Multiplication:
int a = 10;
int b = a * 5;
cout << b << endl; //Prints 50
int c = 12;
c = c * 5;
cout << c << endl; //Prints 60
int d = a * c;
cout << d << endl; //Prints 600
Division:
int a = 10;
int b = a / 5;
cout << b << endl; //Prints 2
int c = 12;
c = c / 5;
cout << c << endl; //Prints 2
//For integers and characters, remainder is discarded, it is the quotient which is left.
//For floats and doubles, there is no remainder and precise division is done.
//For the above division, it will be 2.4 for floats and doubles.
int d = a / c;
cout << d << endl; //Prints 5
I hope that you might have understood all primitive calculations. One can understand it easily if he/she knows algebra. I will not tell you what is algebra because it is not the purpose of this place. Just Google it if you don’t know. However, there are some shorthand methods to some of them, which are really useful. Following are those methods:
int add = 10;
add += 5; //equivalent to "add = add + 5;"
cout << add << endl; //Prints 15
int sub = 10;
sub -= 5; //equivalent to "sub = sub - 5;"
cout << sub << endl; //Prints 5
int mult = 10;
mult *= 5; //equivalent to "mult = mult * 5;"
cout << mult << endl; //Prints 50
int divi = 10;
divi /= 5; //equivalent to "divi = divi / 5;"
cout << divi << endl; //Prints 2
There are two more methods that are very confusing but produces neatness in one’s coding style.
int a = 10;
a++; //equivalent to "a = a + 1;" but with some exceptions
++a; //equivalent to "a = a + 1;" but with some exceptions
cout << a << endl; //Prints 12
//Similar is for subtraction
The first one (a++
) is called a postfix increment operator and the other one (++a
) is called a prefix increment operator. They are not the same and are very much confused with each other. Similar is the case for postfix and prefix decrement operators (i.e, a--
, --a
). This can be proved by the code below:
int print = 25;
//Method 1.
//cout << print = print + 1 << endl; //This gives error and so, is not possible.
//Method 2.
cout << print++ << endl; //Prints 25
//Method 3.
cout << ++print << endl; //Prints 27
Awww… What’s that? It’s a strange-looking code! But let me explain all this. Both postfix and prefix increment and decrement methods can be placed with variables in a complete C++ code sentence as well. Same is not true for a = a + 1;
or a = a - 1;
. In the 2nd method, print
is first printed out and then, incremented by 1. So, it prints 25 and then, its value is changed to 26 i.e print = print + 1;
is done after being printed. In the 3rd method, print
is first incremented by 1 and then, is printed out i.e print = print + 1;
is done before being printed (Remember, 2nd method already changed its value to 26 and so, in the 3rd method, 27 is printed out). The equivalent code to this is given below:
int print = 25;
//"cout << print++ << endl;" is equivalent to
cout << print << endl; //Prints 25
print = print + 1;
//"cout << ++print << endl;" is equivalent to
print = print + 1;
cout << print << endl; //Prints 27
There is another thing. We can have remainder instead of the quotient, in dividing numbers by a special operator called modulo operator, represented by %.
int a = 14;
int b = 5;
cout << a / b << endl; //Prints 2, we can do temporary calculations and print them.
cout << a % b << endl; //Prints 4 which is the remainder obtained by dividing a by b.
a %= a; //a is now, 0.
b %= 3; //b is now, 2.
This post is too lengthy to explain all basic arithmetics in C++. But length doesn’t matter! It is your motivation and inspiration that matters!