C++ Programming Code Examples
C++ > Beginners Lab Assignments Code Examples
Relational Operators in C++ programming
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* Relational Operators in C++ programming
This category of the operators is used to compare different values. The result of the operation is a Boolean value (true or false). The relational operators are used in the following form Operand1 operator Operand2, where operands can be some variables or literals that are compared. Here is the list of relational operators: */
operator description
== Equal to Returns true if the operands are equal. Otherwise false.
!= Not equal to Returns true if the operands are not equal. Otherwise false.
< Less than Returns true if the operand1 is less than operand2. Otherwise false.
> Greater than Returns true if the operand1 is greater than operand2. Otherwise false.
<= Less than or equal to Returns true if the operand1 is less than operand2 or equal to operand 2. Otherwise false.
>= Greater than or equal to Returns true if the operand1 is greater than operand2 or equal to operand 2. Otherwise false.
int three = 3;
int five = 5;
cout << " 3 is equal to 5 = " << (three == five) << endl;
cout << " 3 is not equal to 5 = " << (three != five) << endl;
cout << " 3 is less than 5 = " << (three < five) << endl;
cout << " 3 is greater than 5 = " << (three > five) << endl;
cout << " 3 is not less than 5 = " << (three >= five) << endl;
cout << " 3 is not greater than 5 = " << (three <= five) << endl;
Comments in C++
The C++ comments are statements that are not executed by the compiler. The comments in C++ programming can be used to provide explanation of the code, variable, method or class. If we write comments on our code, it will be easier for us to understand the code in the future. Also, it will be easier for your fellow developers to understand the code. By the help of comments, you can hide the program code also. There are two types of comments in C++:
• Single Line comment
• Multi Line comment
Syntax for Single Line Comment in C++
/* This is a comment */
Syntax for Multi Line Comment in C++
/* C++ comments can also
* span multiple lines
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* program to illustrate use comments in C++ language */
#include <ostream>
using namespace std;
int main()
{
int x = 11; // x is a variable
cout<<x<<"\n";
/* declare and
print variable in C++ */
int x = 35;
cout<<x<<"\n";
// This is a comment
cout << "Hello World!";
/* Multiline Comments
in C++ */
}
Relational Operators in C++
A relational operator is used to check the relationship between two operands. C++ Relational Operators are used to relate or compare given operands. Relational operations are like checking if two operands are equal or not equal, greater or lesser, etc.
Relational Operators are also called Comparison Operators.
• == Is Equal To 4 == 9 gives us false
• != Not Equal To 4 != 9 gives us true
• > Greater Than 4 > 9 gives us false
• < Less Than 4 < 9 gives us true
• >= Greater Than or Equal To 4 >= 9 give us false
• <= Less Than or Equal To 4 <= 9 gives us true
==
Equal To Operator (==) is used to compare both operands and returns 1 if both are equal or the same, and 0 represents the operands that are not equal.
The equal to == operator returns
true  if both the operands are equal or the same
false  if the operands are unequal
int x = 10;
int y = 15;
int z = 10;
x == y // false
x == z // true
The relational operator == is not the same as the assignment operator =. The assignment operator = assigns a value to a variable, constant, array, or vector. It does not compare two operands.
!=
Not Equal To Operator (!=) is the opposite of the Equal To Operator and is represented as the (!=) operator. The Not Equal To Operator compares two operands and returns 1 if both operands are not the same; otherwise, it returns 0.
The not equal to != operator returns
true  if both operands are unequal
false  if both operands are equal.
int x = 10;
int y = 15;
int z = 10;
x != y // true
x != z // false
>
Greater than Operator (>) checks the value of the left operand is greater than the right operand, and if the statement is true, the operator is said to be the Greater Than Operator.
The greater than > operator returns
true  if the left operand is greater than the right
false  if the left operand is less than the right
int x = 10;
int y = 15;
x > y // false
y > x // true
<
Less than Operator (<) is used to check whether the value of the left operand is less than the right operand, and if the statement is true, the operator is known as the Less than Operator.
The less than operator < returns
true  if the left operand is less than the right
false  if the left operand is greater than right
int x = 10;
int y = 15;
x < y // true
y < x // false
>=
Greater than Equal To Operator (>=) checks whether the left operand's value is greater than or equal to the right operand. If the statement is true, the operator is said to be the Greater than Equal to Operator.
The greater than or equal to >= operator returns
true  if the left operand is either greater than or equal to the right
false  if the left operand is less than the right
int x = 10;
int y = 15;
int z = 10;
x >= y // false
y >= x // true
z >= x // true
<=
Less than Equal To Operator (<=) checks whether the value of the left operand is less than or equal to the right operand, and if the statement is true, the operator is said to be the Less than Equal To Operator.
The less than or equal to operator <= returns
true  if the left operand is either less than or equal to the right
false  if the left operand is greater than right
int x = 10;
int y = 15;
x > y // false
y > x // true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* Relational Operators are used for the comparison of the values of two operands. For example, checking if one operand is equal to the other operand or not, an operand is greater than the other operand or not, etc. Some of the relational operators are (==, >= , <= ). */
#include <iostream>
using namespace std;
main() {
int a = 21;
int b = 10;
int c ;
if( a == b ) {
cout << "Line 1  a is equal to b" << endl ;
} else {
cout << "Line 1  a is not equal to b" << endl ;
}
if( a < b ) {
cout << "Line 2  a is less than b" << endl ;
} else {
cout << "Line 2  a is not less than b" << endl ;
}
if( a > b ) {
cout << "Line 3  a is greater than b" << endl ;
} else {
cout << "Line 3  a is not greater than b" << endl ;
}
/* Let's change the values of a and b */
a = 5;
b = 20;
if( a <= b ) {
cout << "Line 4  a is either less than \ or equal to b" << endl ;
}
if( b >= a ) {
cout << "Line 5  b is either greater than \ or equal to b" << endl ;
}
return 0;
}
Arithmetic Operators in C++
Arithmetic Operator is used to performing mathematical operations such as addition, subtraction, multiplication, division, modulus, etc., on the given operands. For example: 6 + 3 = 9, 5  3 = 2, 3 * 4 = 12, etc. are the examples of arithmetic operators. Let's discuss the different types of Arithmetic Operators in the C programming.
+
Plus Operator is a simple Plus (+) Operator used to add two given operands. We can use Plus Operator with different data types such as integer, float, long, double, enumerated and string type data to add the given operand.

The minus operator is denoted by the minus () symbol. It is used to return the subtraction of the first number from the second number. The data type of the given number can be different types, such as int, float, double, long double, etc., in the programing language.
*
The multiplication operator is represented as an asterisk (*) symbol, and it is used to return the product of n1 and n2 numbers. The data type of the given number can be different types such as int, float, and double in the C programing language.
/
The division operator is an arithmetic operator that divides the first (n1) by the second (n2) number. Using division operator (/), we can divide the int, float, double and long data types variables.
%
The modulus operator is represented by the percentage sign (%), and it is used to return the remainder by dividing the first number by the second number.
++
Increment Operator is the type of Arithmetic operator, which is denoted by double plus (++) operator. It is used to increase the integer value by 1.

Decrement Operator is denoted by the double minus () symbol, which decreases the operand value by 1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* Perhaps you have warm memories of doing arithmetic drills in grade school. You can give that same pleasure to your computer. C++ uses operators to do arithmetic. It provides operators for five basic arithmetic calculations: addition, subtraction, multiplication, division, and taking the modulus. Each of these operators uses two values (called operands) to calculate a final answer. Together, the operator and its operands constitute an expression. */
#include <iostream>
using namespace std;
int main() {
int a, b;
a = 7;
b = 2;
// printing the sum of a and b
cout << "a + b = " << (a + b) << endl;
// printing the difference of a and b
cout << "a  b = " << (a  b) << endl;
// printing the product of a and b
cout << "a * b = " << (a * b) << endl;
// printing the division of a by b
cout << "a / b = " << (a / b) << endl;
// printing the modulo of a by b
cout << "a % b = " << (a % b) << endl;
return 0;
}
Standard Output Stream (cout) in C++
The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout.
Syntax for cout in C++
cout << var_name;
//or
cout << "Some String";
<<
is the insertion operator
var_name
is usually a variable, but can also be an array element or elements of containers like vectors, lists, maps, etc.
The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output".
The cout object is used along with the insertion operator << in order to display a stream of characters.
The << operator can be used more than once with a combination of variables, strings, and manipulators.
cout is used for displaying data on the screen. The operator << called as insertion operator or put to operator. The Insertion operator can be overloaded. Insertion operator is similar to the printf() operation in C. cout is the object of ostream class. Data flow direction is from variable to output device. Multiple outputs can be displayed using cout.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* standard output stream (cout) in C++ language */
#include <iostream>
using namespace std;
int main() {
string str = "Do not interrupt me";
char ch = 'm';
// use cout with write()
cout.write(str,6);
cout << endl;
// use cout with put()
cout.put(ch);
return 0;
}
The array of integers indicating the marks of the students is given, U have to calculate the percentile of the students aaccording to this rule: the percentile of a student is the %of no