C++ Operators - I
The following topics are covered in this section:
These operators are used to perform basic mathematical operations like addition, subtraction, division and multiplication.
Operator Symbol | Operation performed |
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulo operator (Remainder after division) |
The modulo operator is not used to calculate the percentage. It is used to find the remainder after integer division. For example: 6 % 4 = 2 (because dividing 6 by 4 produces a remainder of 2).
The modulo operator can operate only on integers. Since the
modulo operator is used to determine the remainder after division, it is also
called the remainder operator.
Arithmetic operators are binary operators since they need two quantities (or
operands) to perform an operation.
#include <iostream.h> |
In the program, we declare two integers ‘num1’ and ‘num2’ and obtain their values from the user through the statement:
cin>>num1>>num2;
This is a method of obtaining multiple inputs using a single statement. The above statement is equivalent to writing:
cin>>num1;
cin>>num2;
The two numbers, when entered by the user, can be separated by a space or by a new-line (i.e. the first number is typed and then after pressing the ‘enter’ key the second number is typed).
When you run the program you would get the following on your screen:
Enter the two numbers : 8 4
The product is : 32The sum is : 12The difference is : 4The quotient is : 2The remainder is : 0
Something is not right in this output; the results are correct but the display is on a single line. To display the output in an organized manner, the program should print each output on a new line. For this purpose of formatting the output C++ provides us with ‘escape sequence’.
Escape Sequences/ Backslash Character Constants
If you remember, whatever you type within double quotes following cout<<, will be printed as it is on the screen. There is a problem in case you want to print a new line, or you want to use tabs (because whatever you type within double quotes will be displayed directly on the screen).
To solve this problem, escape sequences were developed. Just as the name implies, these escape sequence characters are used to escape from the normal sequence of events. An escape sequence always begins with a backslash ( \ ). For a new line, the escape sequence is \n (n for new line). If you want to push the tab setting then \t should be used (t for tab).
The modified program for doing simple arithmetic operations is as follows:
#include <iostream.h> |
When you run the program you would get the following on your screen:
Enter the two numbers : 8 4
The product is : 32
The sum is : 12
The difference is : 4
The quotient is : 2
The remainder is : 0
There are a few other useful escape sequence characters as well:
Escape Sequence |
Meaning |
\n |
new line |
\t |
horizontal tab |
\a |
bell sound |
\\ |
Backslash |
\? |
question mark (?) |
\" |
double quotation |
\’ |
Apostrophe |
\0 |
Null character (used in strings) |
Though escape sequences consist of 2 characters they represent only a single character (for example: \? represents the question mark character). Escape sequences are character constants and are denoted as ‘\\’, ‘\?’, ‘\0’ etc.
Beware: ‘\0’ is not equivalent to zero. ‘\0’ represents a null character (used in strings).
Try it: Use the above escape sequences in a C++ program to see their effects. The \a escape sequence will literally make a sound of a bell.
Remember: Escape sequences always start with a backslash ( \ ) and have to be followed by valid characters (like n, a, t etc…). If you happen to use some other character (like w), the compiler will display a warning message (the program will run and usually the compiler will ignore the character or display the character on screen).
Coming back to the program with arithmetic operators; what would be the output if 5 and 2 are entered as the two numbers? What will the quotient and remainder be? The result will give a quotient of 2 and a remainder of 1. But isn’t 5/2 = 2.5?
The answer to this lies in the declaration of the variables ‘num1’ and ‘num2’. Since these two variables are declared as integers, all the results of arithmetic operations will also be in the integer format. Suppose ‘num1’ and ‘num2’ are declared as floating point data type, then the result of division would be 2.5 instead of 2. In this case, what do you think will be the remainder? Will it be zero always?
Remember: The modulo (%) operator can operate only on two integers and not on any other data type. If the remainder operator is used on floating point numbers, the compiler will produce an error message. The other arithmetic operators can be used on integers as well as floating-point numbers.
Copyright © 2004 Sethu Subramanian All rights reserved.