C++ Codes - Basic to Advanced π»
Open in Telegram
π‘ Daily C++ Codes with Output & Logic Any queries, message π @sai7981 π€ Get instant code explanations: @cpp_codes_bot π Join now: @cpp_code_snippets #cpp #dsa #coding #programming
Show more3 188
Subscribers
No data24 hours
+357 days
+12030 days
Posts Archive
Unmasking the Magic: Bitwise Operators in C++ π
#include <iostream>
int main() {
int a = 5;
int b = 3;
int and_result = a & b;
int or_result = a | b;
int xor_result = a ^ b;
int not_result_a = ~a;
int left_shift = a << 1;
int right_shift = a >> 1;
std::cout << "a = " << a << ", b = " << b << std::endl;
std::cout << "AND: " << and_result << std::endl;
std::cout << "OR: " << or_result << std::endl;
std::cout << "XOR: " << xor_result << std::endl;
std::cout << "NOT a: " << not_result_a << std::endl;
std::cout << "Left Shift (a << 1): " << left_shift << std::endl;
std::cout << "Right Shift (a >> 1): " << right_shift << std::endl;
return 0;
}Are you smarter than a C++ Logical Operator? π€
#include <iostream>
int main() {
int x = 5;
int y = 10;
bool resultAnd = (x > 0) && (y < 20);
bool resultOr = (x > 10) || (y < 15);
bool resultNot = !(x == y);
std::cout << "AND: " << resultAnd << std::endl;
std::cout << "OR: " << resultOr << std::endl;
std::cout << "NOT: " << resultNot << std::endl;
return 0;
}Is One Number Greater Than Another? π€ Mastering Relational Operators in C++!
#include <iostream>
int main() {
int num1, num2;
std::cout << "Enter two integers: ";
std::cin >> num1 >> num2;
std::cout << "num1 > num2: " << (num1 > num2) << std::endl;
std::cout << "num1 < num2: " << (num1 < num2) << std::endl;
std::cout << "num1 >= num2: " << (num1 >= num2) << std::endl;
std::cout << "num1 <= num2: " << (num1 <= num2) << std::endl;
std::cout << "num1 == num2: " << (num1 == num2) << std::endl;
std::cout << "num1 != num2: " << (num1 != num2) << std::endl;
return 0;
}π’ Weβd love your feedback!
Help us improve the content we share on this channel.
Please take a minute to fill out this short form π
π https://forms.gle/BJYzQTGTLgQ44S4fA
Unlocking C++: Mastering Increment and Decrement Operators! π
#include <iostream>
int main() {
int num = 10;
std::cout << "Initial value: " << num << std::endl;
num++;
std::cout << "After increment: " << num << std::endl;
num--;
std::cout << "After decrement: " << num << std::endl;
++num;
std::cout << "After pre-increment: " << num << std::endl;
--num;
std::cout << "After pre-decrement: " << num << std::endl;
int result = num++;
std::cout << "Post-increment result: " << result << ", num: " << num << std::endl;
result = ++num;
std::cout << "Pre-increment result: " << result << ", num: " << num << std::endl;
return 0;
}Math Magic: Unleashing Arithmetic Operators in C++! βββοΈβ
#include <iostream>
int main() {
int num1, num2;
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
int sum = num1 + num2;
int difference = num1 - num2;
int product = num1 * num2;
float quotient = (float)num1 / num2;
int remainder = num1 % num2;
std::cout << "Sum: " << sum << std::endl;
std::cout << "Difference: " << difference << std::endl;
std::cout << "Product: " << product << std::endl;
std::cout << "Quotient: " << quotient << std::endl;
std::cout << "Remainder: " << remainder << std::endl;
return 0;
}corresponding bits in the operands is 1.
- `^` (Bitwise XOR): Performs a bitwise XOR (exclusive OR) operation. Each bit in the result is 1 if the corresponding bits in the operands are different.
- `~` (Bitwise NOT): Inverts the bits of the operand. Turns 0s to 1s and 1s to 0s.
- `<<` (Left Shift): Shifts the bits of the operand to the left by a specified number of positions. Equivalent to multiplying by powers of 2.
- `>>` (Right Shift): Shifts the bits of the operand to the right by a specified number of positions. Equivalent to dividing by powers of 2.
π‘ **Operator Precedence:** Just like in math, operators have precedence. For example, multiplication and division are performed before addition and subtraction. Use parentheses `()` to explicitly control the order of operations if you're unsure or want to make your code clearer. β
**Example Code Snippet:**
#include <iostream>
int main() {
int x = 10;
int y = 5;
int sum = x + y; // Arithmetic operator (+)
std::cout << "Sum: " << sum << std::endl;
bool isEqual = (x == y); // Relational operator (==)
std::cout << "x == y: " << isEqual << std::endl;
bool combinedCondition = (x > 5 && y < 10); // Logical operator (&&)
std::cout << "x > 5 && y < 10: " << combinedCondition << std::endl;
int bitwiseAnd = x & y; // Bitwise operator (&) - Be careful with binary conversions.
std::cout << "Bitwise AND: " << bitwiseAnd << std::endl;
return 0;
}
Practice using these operators in your own programs! The more you experiment, the better you'll understand how they work. Happy coding! π
Keep learning, and you'll become a C++ operator pro in no time! πͺHey there, future C++ coders! π Let's dive into the fascinating world of "Operators" in C++. Think of operators as special symbols that tell the C++ compiler to perform specific operations on data. They're like the verbs in the language of programming!
π§
Operators allow us to manipulate variables, make decisions, and control the flow of our programs. Let's explore some key types: arithmetic, logical, relational, and bitwise operators.
**Arithmetic Operators: The Math Wizards!** βββ
These operators are your go-to tools for performing mathematical calculations.
- `+` (Addition): Adds two operands. Example: `int sum = 5 + 3;` `sum` will be 8.
- `-` (Subtraction): Subtracts the second operand from the first. Example: `int difference = 10 - 4;` `difference` will be 6.
- `` (Multiplication): Multiplies two operands. Example: `int product = 6 7;` `product` will be 42.
- `/` (Division): Divides the first operand by the second. Example: `int quotient = 20 / 5;` `quotient` will be 4. β οΈ Be careful with integer division! If both operands are integers, the result will be an integer, discarding any remainder. `int result = 7 / 2;` `result` will be 3 (not 3.5).
- `%` (Modulo): Returns the remainder of a division. Example: `int remainder = 17 % 5;` `remainder` will be 2.
**Relational Operators: Comparing Values!** == != > < >= <=
These operators let you compare two values and determine their relationship to each other. They always return a boolean value: `true` (1) or `false` (0).
- `==` (Equal to): Checks if two operands are equal. Example: `bool isEqual = (5 == 5);` `isEqual` will be `true`.
- `!=` (Not equal to): Checks if two operands are not equal. Example: `bool isNotEqual = (5 != 3);` `isNotEqual` will be `true`.
- `>` (Greater than): Checks if the first operand is greater than the second. Example: `bool isGreater = (10 > 5);` `isGreater` will be `true`.
- `<` (Less than): Checks if the first operand is less than the second. Example: `bool isLess = (2 < 7);` `isLess` will be `true`.
- `>=` (Greater than or equal to): Checks if the first operand is greater than or equal to the second.
- `<=` (Less than or equal to): Checks if the first operand is less than or equal to the second.
**Logical Operators: Combining Conditions!** && || !
These operators are used to combine or modify boolean expressions. They're crucial for making complex decisions in your code.
- `&&` (Logical AND): Returns `true` if both operands are `true`. Otherwise, it returns `false`. Example: `bool result = (true && true);` `result` will be `true`. `bool result2 = (true && false);` `result2` will be `false`.
- `||` (Logical OR): Returns `true` if at least one of the operands is `true`. It returns `false` only if both operands are `false`. Example: `bool result = (true || false);` `result` will be `true`. `bool result2 = (false || false);` `result2` will be `false`.
- `!` (Logical NOT): Negates the operand. If the operand is `true`, it returns `false`, and vice versa. Example: `bool value = true; bool negatedValue = !value;` `negatedValue` will be `false`.
**Bitwise Operators: Working with Bits!** & | ^ ~ << >>
These operators work directly on the individual bits of integer data. They're often used in low-level programming and tasks like manipulating hardware or optimizing performance. β οΈ They require a good understanding of binary numbers.
- `&` (Bitwise AND): Performs a bitwise AND operation. Each bit in the result is 1 only if the corresponding bits in both operands are 1. Example: `int a = 5; // 0101 in binary; int b = 3; // 0011 in binary; int result = a & b; // 0001 in binary, which is 1 in decimal.`
- `|` (Bitwise OR): Performs a bitwise OR operation. Each bit in the result is 1 if at least one of the
Decoding Data: Can you display different data types in C++?
#include <iostream>
int main() {
int integer_value = 10;
double double_value = 3.14;
char char_value = 'A';
bool bool_value = true;
std::cout << "Integer: " << integer_value << std::endl;
std::cout << "Double: " << double_value << std::endl;
std::cout << "Character: " << char_value << std::endl;
std::cout << "Boolean: " << bool_value << std::endl;
return 0;
}Mixed Math Mayhem: How C++ Handles Different Data Types!
#include <iostream>
int main() {
int integerValue = 5;
double doubleValue = 2.5;
double result1 = integerValue + doubleValue;
std::cout << "Integer + Double = " << result1 << std::endl;
int result2 = integerValue + static_cast<int>(doubleValue);
std::cout << "Integer + (Int)Double = " << result2 << std::endl;
double result3 = static_cast<double>(integerValue) / 2;
std::cout << "(Double)Integer / 2 = " << result3 << std::endl;
return 0;
}Transforming Numbers: Can you change a double into an integer precisely in C++?
#include <iostream>
int main() {
double pi = 3.14159;
int integer_pi = static_cast<int>(pi);
std::cout << "Original double: " << pi << std::endl;
std::cout << "Integer value: " << integer_pi << std::endl;
double height = 170.5;
int rounded_height = static_cast<int>(height + 0.5);
std::cout << "Original height: " << height << std::endl;
std::cout << "Rounded height: " << rounded_height << std::endl;
return 0;
}π€ How does C++ handle automatic number conversions?
#include <iostream>
int main() {
float floatValue = 3.14;
int intValue = floatValue;
char charValue = 'A';
int charToInt = charValue;
std::cout << "Float to Int: " << intValue << std::endl;
std::cout << "Char to Int: " << charToInt << std::endl;
return 0;
}