0% found this document useful (0 votes)
3 views4 pages

Sample Programs - 2

C++

Uploaded by

gunjotikaraditya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Sample Programs - 2

C++

Uploaded by

gunjotikaraditya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Arithmetic operators:

#include <iostream>
using namespace std;

int main() {
int a = 10, b = 3;

cout << "Addition: " << a + b << endl; // 13


cout << "Subtraction: " << a - b << endl; // 7
cout << "Multiplication: " << a * b << endl; // 30
cout << "Division: " << a / b << endl; // 3 (integer division)
cout << "Modulus: " << a % b << endl; // 1

return 0;
}

Relational Operators:

#include <iostream>
using namespace std;

int main() {
int x = 5, y = 8;

cout << (x == y) << endl; // false (0)


cout << (x != y) << endl; // true (1)
cout << (x > y) << endl; // false (0)
cout << (x < y) << endl; // true (1)

return 0;
}

Logical operators:

#include <iostream>
using namespace std;

int main() {
int age = 20;
bool hasID = true;

// AND (&&)
cout << (age >= 18 && hasID) << endl; // true

// OR (||)
cout << (age < 18 || hasID) << endl; // true

// NOT (!)
cout << !(age >= 18) << endl; // false

return 0;
}

Unary Operators:

#include <iostream>
using namespace std;

int main() {

int a = 10;
int b = 6;

// Address-of operator (&)


int* ptr = &a;
cout << "Address of a: " << &a << endl;
cout << "Value of ptr: " << ptr << endl;

// ----- * (Pointer dereference) -----

cout << "Value pointed to by ptr: " << *ptr << endl;

// ----- ~ (Bitwise NOT) -----

int c = 5; // binary: 00000101


cout << "~c = " << ~c << endl; // bitwise inversion

// ----- ! (Logical NOT) -----

bool flag1 = true;


bool flag2 = false;

cout << "!flag1 = " << !flag1 << endl;


cout << "!flag2 = " << !flag2 << endl;

// Logical NOT with integers


int x = 0;
int y = 10;

cout << "!x = " << !x << endl; // true (1)


cout << "!y = " << !y << endl; // false (0)

return 0;
}
Prefix vs. Postfix (++ and --)
int a = 5;
int b = ++a; // a is 6, b is 6 (Increment then assign)

int x = 5;
int y = x++; // x is 6, y is 5 (Assign then increment)

Bitwise operators:

#include <iostream>
#include <bitset> // Useful for printing binary
int main() {
unsigned char a = 5; // 0000 0101
unsigned char b = 9; // 0000 1001

std::cout << "a & b: " << (a & b) << "\n"; // Result: 1 (0000 0001)
std::cout << "a | b: " << (a | b) << "\n"; // Result: 13 (0000 1101)
std::cout << "a ^ b: " << (a ^ b) << "\n"; // Result: 12 (0000 1100)
return 0;
}

Shift Operators:
Left shift:
#include <iostream>
int main() {
int x = 5; // Binary: 0000 0101
int result = x << 1;

// 5 * (2^1) = 5 * 2 = 10
// Binary result: 0000 1010
std::cout << "5 << 1 is: " << result << std::endl;
return 0;
}
Right Shift:

#include <iostream>
int main() {
int x = 40; // Binary: 0010 1000
int result = x >> 3;

// 40 / (2^3) = 40 / 8 = 5
// Binary result: 0000 0101
std::cout << "40 >> 3 is: " << result << std::endl;
return 0;
}

You might also like