Example Operators
Example Operators
Example Operators
Arithmetic Operators
#include <iostream>
using namespace std;
int main() {
int a, b;
a = 7;
b = 2;
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;
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;
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;
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;
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
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>
// 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;
return 0;
}
Output:
AND: 1
OR: 7
XOR: 6
NOT a: -6
Left Shift: 20
Right Shift: 2
Example 2:
#include <iostream>
int main ()
a = a << 2;
b = b >> 2;
return 0;