Centre for Artificial Intelligence Madhav Institute of Technology & Science Submitted to : Submitted by: Mr. Arun Kumar Yash Jain(0901AI231077)
Promblem Solving and Programming <OPERATORS> { } ...
OPERATORS The symbol that are used in C++ programs to form an expression are known as OPERATORS.C++ has a rich set of operators including all C language’s operators and also some new operators. There are three categories of operators in C++. These are : 01: Unary Operators 02: Binary Operators 03: Ternary Operators
OPERATORS T he operators thatr operate a single operand to form an expression are Unary operators.like ++,-- The operators that operate two or more operands are Binary operators. The operators are +,-,*,/,% etc. T he operators that operates minimum or maximum three operands are Ternary operators. O nly one (?) used as substitute of if-else statement. 01 02 03 Unary Operators Binary Operators Ternary Operators
Type of operators in C++ < * Arithmetic Operators * Logical Operators * Bitwise Operators * Relational Operators * Assignment Operators > * }
The operators that helps the programmer in mathematical calculation are Arithmetic Operators. Its include (+) for addition , (-) for subtraction, (/) for division ,(*) for multiplication etc. 01. Arthimetic Operators <example> 2+5= 7 2*7= 14 8/2= 4 8-4= 4 } ..
P rogram for arthimetic operators OUTPUT: Sum: 15 Difference: 5 Product: 50 Quotient: 2 Remainder: 0 #include <iostream> using namespace std; int main() { int num1 = 10, num2 = 5; int sum = num1 + num2; cout << "Sum: " << sum << endl ; int difference = num1 - num2; cout << "Difference: " << difference << endl ; int product = num1 * num2; cout << "Product: " << product << endl ; int quotient = num1 / num2; cout << "Quotient: " << quotient << endl ; int remainder = num1 % num2; cout << "Remainder: " << remainder << endl ; return 0; } } ..
02. Logical Operators T he Operators that help the programmer to connect (combine) two or more expression, are known as Logical Operators. I t include : 01: (&&) logical AND 02: (||) logical OR 03: (!) logical NOT *
P rogram for logical operators OUTPUT; x && y: 0 x && z: 1 x || y: 1 y || z: 1 !x: 0 !y: 1 #include <iostream> using namespace std; int main() { bool x = true, y = false, z = true; // AND operator (&&) cout << "x && y: " << (x && y) << endl ; // false cout << "x && z: " << (x && z) << endl ; // true // OR operator (||) cout << "x || y: " << (x || y) << endl ; // true cout << "y || z: " << (y || z) << endl ; // true // NOT operator (!) cout << "!x: " << !x << endl ; // false cout << "!y: " << !y << endl ; // true return 0; } { } *
03 . Bitwise Operators (~) bitwise complement (<<) left shift (>>) right shift (&) bitwise AND (|) bitwise OR (^) bitwise exclusive Or T he operators which operate a bit level and allows the programmer to manipulate individual bits. T hese are basically used for testing or shifting bits. B itwise operators are: ...
P rogram for Bitwise operators #include <iostream> using namespace std; int main(){ int x = 10, y = 7, z = 0; // Bitwise AND (&) z = x & y; cout << "x & y = " << z << endl ; // Bitwise OR (|) z = x | y; cout << "x | y = " << z << endl ; // Bitwise XOR (^) z = x ^ y; cout << "x ^ y = " << z << endl ; // Bitwise NOT (~) z = ~x; cout << "~x = " << z << endl ; // Left shift (<<) z = x << 2; cout << "x << 2 = " << z << endl ; // Right shift (>>) z = y >> 1; cout << "y >> 1 = " << z << endl ; return 0; } * OUYPUT: x & y = 2 x | y = 15 x ^ y = 13 ~x = -11 x << 2 = 40 y >> 1 = 3
04. Relational Operators Relational operators are used to compare values and determine their relationship. They return a Boolean value of TRUE(1) if the relation is true, and false(0) if it’s not. == equal to X == y != Not equal to X != y > Greater than X > y < Less than X < y >= Greater than or equal to X >= y <= Less than or equal to X <= y
P rogram for relational operators int num1 = 20, num2 = 15; // Equality check if (num1 == num2) { cout << num1 << " is equal to " << num2 << endl ; } else { cout << num1 << " is not equal to " << num2 << endl ; } // Greater than check if (num1 > num2) { cout << num1 << " is greater than " << num2 << endl ; } // Less than check if (num1 < num2) { cout << num1 << " is less than " << num2 << endl ; } // Greater than or equal to check if (num1 >= num2) { cout << num1 << " is greater than or equal to " << num2 << endl ; } // Less than or equal to check if (num1 <= num2) { cout << num1 << " is less than or equal to " << num2 << endl ; } } .. OUTPUT: 20 is not equal to 15 20 is greater than 15 20 is greater than or equal to 15
05. Assignment Operators { } .. * Assignment operators are used to assign values to variables. They store the value on the right side of the operator into the variable on the left side.it include : 01. Simple assign (=) 02. Add and assign (+=) 03. Subtract and assign (-=) 04. Multiply and assign (*=) 05. Divide and assign (/=) 06. Modulo assign (%=)
P rogram for Assignment operators int x = 10; int y = 5; // Simple assignment cout << "Original value of x: " << x << endl ; x = 20; // Assign 20 to x cout << "After assignment: x = " << x << endl ; // Compound assignment operators x += 5; // Equivalent to x = x + 5 cout << "After x += 5: x = " << x << endl ; y -= 2; // Equivalent to y = y - 2 cout << "After y -= 2: y = " << y << endl ; x *= 3; // Equivalent to x = x * 3 cout << "After x *= 3: x = " << x << endl ; y /= 2; // Equivalent to y = y / 2 cout << "After y /= 2: y = " << y << endl ; return 0; } * OUTPUT: Original value of x: 10 After assignment: x = 20 After x += 5: x = 25 After y -= 2: y = 3 After x *= 3: x = 75 After y /= 2: y = 1