Example Operators

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Example 1:

Arithmetic Operators
#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;
}

Output
a+b=9
a-b=5
a * b = 14
a/b=3
a%b=1

Example 2: Increment
and Decrement
Operators
// Working of increment and decrement operators

#include <iostream>
using namespace std;

int main() {
int a = 10, b = 100, result_a, result_b;

// incrementing a by 1 and storing the result in


result_a
result_a = ++a;
cout << "result_a = " << result_a << endl;

// decrementing b by 1 and storing the result in


result_b
result_b = --b;
cout << "result_b = " << result_b << endl;

return 0;
}
Output
result_a = 11
result_b = 99
Example 3:
Assignment Operators
#include <iostream>
using namespace std;

int main() {
int a, b;

// 2 is assigned to a
a = 2;

// 7 is assigned to b
b = 7;

cout << "a = " << a << endl;


cout << "b = " << b << endl;
cout << "\nAfter a += b;" << endl;

// assigning the sum of a and b to a


a += b; // a = a +b
cout << "a = " << a << endl;
return 0;
}

Output
a=2
b=7

After a += b;
a=9

Example 4: Relational
Operators
#include <iostream>
using namespace std;

int main() {
int a, b;
a = 3;
b = 5;
bool result;
result = (a == b); // false
cout << "3 == 5 is " << result << endl;

result = (a != b); // true


cout << "3 != 5 is " << result << endl;

result = a > b; // false


cout << "3 > 5 is " << result << endl;

result = a < b; // true


cout << "3 < 5 is " << result << endl;

result = a >= b; // false


cout << "3 >= 5 is " << result << endl;

result = a <= b; // true


cout << "3 <= 5 is " << result << endl;

return 0;
}
Output
3 == 5 is 0
3 != 5 is 1
3 > 5 is 0
3 < 5 is 1
3 >= 5 is 0
3 <= 5 is 1

Example 5: Logical
Operators
#include <iostream>
using namespace std;

int main() {
bool result;

result = (3 != 5) && (3 < 5); // true


cout << "(3 != 5) && (3 < 5) is " << result <<
endl;

result = (3 == 5) && (3 < 5); // false


cout << "(3 == 5) && (3 < 5) is " << result <<
endl;
result = (3 == 5) && (3 > 5); // false
cout << "(3 == 5) && (3 > 5) is " << result <<
endl;

result = (3 != 5) || (3 < 5); // true


cout << "(3 != 5) || (3 < 5) is " << result << endl;

result = (3 != 5) || (3 > 5); // true


cout << "(3 != 5) || (3 > 5) is " << result << endl;

result = (3 == 5) || (3 > 5); // false


cout << "(3 == 5) || (3 > 5) is " << result << endl;

result = !(5 == 2); // true


cout << "!(5 == 2) is " << result << endl;

result = !(5 == 5); // false


cout << "!(5 == 5) is " << result << endl;

return 0;
}

Output
(3 != 5) && (3 < 5) is 1
(3 == 5) && (3 < 5) is 0
(3 == 5) && (3 > 5) is 0
(3 != 5) || (3 < 5) is 1
(3 != 5) || (3 > 5) is 1
(3 == 5) || (3 > 5) is 0
!(5 == 2) is 1
!(5 == 5) is 0

Example: Type Conversion (Casting)


#include <iostream>
using namespace std;
int main()
{
cout << "static_cast<int>(7.9) = " << static_cast<int>(7.9) << endl;
cout << "static_cast<int>(3.3) = " << static_cast<int>(3.3) << endl;
cout << "static_cast<double>(25) = " << static_cast<double>(25) << endl;
cout << "static_cast<double>(5 + 3) = " << static_cast<double>(5 + 3) << endl;
cout << "static_cast<double>(15) / 2 = " << static_cast<double>(15) / 2 << endl;
cout << "static_cast<double>(15 / 2) = " <<static_cast<double>(15 / 2) << endl;
cout << "static_cast<int>(7.8 + static_cast<double>(15) / 2) = " << static_cast<int>(7.8 +
static_cast<double>(15) / 2) << endl;
//return 0;
system("pause");
}

Output:
static_cast<int>(7.9) = 7
static_cast<int>(3.3) = 3
static_cast<double>(25) = 25.0
static_cast<double>(5 + 3) = 8.0
static_cast<double>(15) / 2 = 7.5
static_cast<double>(15 / 2) = 7.5
static_cast<int>(7.8 + static_cast<double>(15) / 2) = 15
example:
// This program illustrates how data in the variables are
// manipulated.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int num1, num2;
double sale;
char first;
string str;
num1 = 4;
cout << "num1 = " << num1 << endl;
num2 = 4 * 5 - 11;
cout << "num2 = " << num2 << endl;
sale = 0.02 * 1000;
cout << "sale = " << sale << endl;
first = 'D';
cout << "first = " << first << endl;
str = "It is a sunny day.";
cout << "str = " << str << endl;
//return 0;
system("pause");
}

output:
num1 = 4
num2 = 9
sale = 20
first = D
str = It is a sunny day
The following program shows the effect of the preceding input statements:
// This program illustrates how input statements work.
#include <iostream>
using namespace std;
int main()
{
int y;
int x;
cout << "Enter two integers separated by one or more spaces: ";
cin >> y >> x;
cout << endl;
cout <<" y = " << y << endl;
cout << "x= " << x << endl;
return 0;
}output:
Enter two integers separated by one or more spaces: 23 7
x = 23
y=7

(bitwise operator )
// C++ Program to demonstrate
// Bitwise Operator
#include <iostream>

using namespace std;

// Main function
int main()
{
int a = 5; // 101
int b = 3; // 011

// Bitwise AND
int bitwise_and = a & b;

// Bitwise OR
int bitwise_or = a | b;

// Bitwise XOR
int bitwise_xor = a ^ b;

// Bitwise NOT
int bitwise_not = ~a;

// Bitwise Left Shift


int left_shift = a << 2;

// Bitwise Right Shift


int right_shift = a >> 1;

// Printing the Results of


// Bitwise Operators
cout << "AND: " << bitwise_and << endl;
cout << "OR: " << bitwise_or << endl;
cout << "XOR: " << bitwise_xor << endl;
cout << "NOT a: " << bitwise_not << endl;
cout << "Left Shift: " << left_shift << endl;
cout << "Right Shift: " << right_shift << endl;

return 0;
}
Output:
AND: 1
OR: 7
XOR: 6
NOT a: -6
Left Shift: 20
Right Shift: 2

Example 2:
#include <iostream>

using namespace std;

int main ()

int a = 10, b = 13;


cout << "Bitwise AND: \t" << (a & b) << endl;

cout << "Bitwise OR: \t" << (a | b) << endl;

cout << "Bitwise X-OR: \t" << (a ^ b) << endl;

cout << "Bitwise NOt A: \t" << ~a << endl;

cout << "Bitwise Not B: \t" << ~b << endl;

a = a << 2;

cout << "Bitwise leftshift of a:\t" << a << endl;

b = b >> 2;

cout << "Bitwise Rightshift of b:\t" << b << endl;

return 0;

You might also like