The document contains multiple C++ code snippets demonstrating various programming concepts including pre-increment and pre-decrement operations, logical operators, static variables, pointers, function overloading, bitwise operations, array declaration and traversal, summing elements of two arrays, and checking for even or odd numbers. Each snippet is self-contained and illustrates specific functionalities and syntax in C++. The examples are designed to help users understand fundamental programming techniques in C++.
The document contains multiple C++ code snippets demonstrating various programming concepts including pre-increment and pre-decrement operations, logical operators, static variables, pointers, function overloading, bitwise operations, array declaration and traversal, summing elements of two arrays, and checking for even or odd numbers. Each snippet is self-contained and illustrates specific functionalities and syntax in C++. The examples are designed to help users understand fundamental programming techniques in C++.
#include <iostream> using namespace std; int main() { int x = 15; cout << ++x << endl; cout << --x << endl; return 0; }
(4): Logical Operators in C++ (AND, OR, NOT)
#include <iostream> using namespace std; int main() { int x = 10, y = 20; bool result; result = (x > 5) && (y > 15); cout << "Result of (x > 5) AND (y > 15): " << result << endl; result = (x > 15) || (y > 15); cout << "Result of (x > 15) OR (y > 15): " << result << endl; result = !(x > 5); cout << "Result of NOT (x > 5): " << result << endl; return 0; } (5): Understanding Static Variables in C++ Functions #include <iostream> using namespace std; void staticCounter() { static int counter = 0; ++counter; cout << "The static counter value is: " << counter << endl; } int main() { staticCounter(); staticCounter(); staticCounter(); return 0; } (6): Pointers and Memory Addresses in C++ #include <iostream> using namespace std; int main() { int x = 1, y = 5; cout << &x << endl; cout << &y << endl; int *ptr; ptr = &x; cout << ptr << endl; ptr = &y; cout << ptr << endl; return 0; } (7): Function Overloading in C++ #include <iostream> using namespace std; int sum(int a, int b) { return a + b; } int sum(int a, int b, int c) { return a + b + c; } int main() { int x = 10, y = 20, z = 30; double p = 5.5, q = 3.3; cout << "Sum of x and y: " << sum(x, y) << endl; cout << "Sum of x, y and z: " << sum(x, y, z) << endl; return 0; } (8): Bitwise AND and Memory Address Display in C++ #include <iostream> using namespace std; int main() { bool a = true, b = true, c; int x = 2; c = a & b; cout << c << endl; cout << x << endl; cout << &x << endl; return 0; } (9): Array Declaration and Traversal in C++ #include <iostream> using namespace std; int main() { int arr[5] = {10, 20, 30, 40, 50}; cout << "The elements of the array are:\n"; for (int i = 0; i < 5; i++) cout << arr[i] << "\t"; cout << endl; return 0; } (10): Initializing and Displaying an Array in C++ #include <iostream> using namespace std; int main() { int x[] = {5, 8, 7}; cout << "The list is\n"; for (int i = 0; i < 3; i++) cout << x[i] << "\t"; cout << endl; return 0; } (11): Summing Corresponding Elements of Two Arrays #include <iostream> using namespace std; int main() { const int SIZE = 5; int array1[SIZE] = {1, 2, 3, 4, 5}; int array2[SIZE] = {10, 20, 30, 40, 50}; int sumArray[SIZE]; for (int i = 0; i < SIZE; i++) sumArray[i] = array1[i] + array2[i]; cout << "The sum of the two arrays is:\n"; for (int i = 0; i < SIZE; i++) cout << sumArray[i] << "\t"; cout << endl; return 0; } (12): Checking Even or Odd Number in C++ #include <iostream> using namespace std; int main() { int number; cout << "Enter a number: "; cin >> number; if (number % 2 == 0) cout << number << " is even." << endl; else cout << number << " is odd." << endl; return 0; } (13): Even or Odd Check Using Ternary Operator #include <iostream> using namespace std; int main() { int number; cout << "Enter a number: "; cin >> number; string result = (number % 2 == 0) ? "even" : "odd"; cout << number << " is " << result << "." << endl; return 0; }