In this Problem, you’ll learn about different types of operators in C#, their syntax and how to use them with the help of examples.
Operators are symbols that perform operations on variables and values. For example, + is an operator used for addition, while * is also an operator used for multiplication.
Operators in C# can be classified into 6 types:
In C#, Operators can also categorized based upon Number of Operands :
We will discuss about arithmetic operators and comparision operators and rest we will study in next task.
Arithmetic Operators:
Arithmetic operators are used to perform arithmetic operations on variables and data. For example,
a + b;
Here, the + operator is used to add two variables a and b. Similarly, there are various other arithmetic operators in C#.
Operator Operation + Addition - Subtraction * Multiplication / Division % Modulo Operation (Remainder after division)
The ones falling into the category of Unary Operators are:
Comparison Operators
Comparison operators are used for comparison of two values. Let’s see them one by one:
Example
// C# program to demonstrate the working
// of Arithmetical and Comparison Operators
using System;
class Interviewbit {
// Main Function
static void Main(string[] args)
{
int res;
int x = 10, y = 5;
// Addition
res = (x + y);
Console.WriteLine("Addition Operator: " + res);
// Subtraction
res = (x - y);
Console.WriteLine("Subtraction Operator: " + res);
// Multiplication
res = (x * y);
Console.WriteLine("Multiplication Operator: "+ re);
// Division
res = (x / y);
Console.WriteLine("Division Operator: " + res);
// Modulo
res = (x % y);
Console.WriteLine("Modulo Operator: " + res);
bool result;
x = 5;
y = 10;
// Equal to Operator
result = (x == y);
Console.WriteLine("Equal to Operator: " + result);
// Greater than Operator
result = (x > y);
Console.WriteLine("Greater than Operator: " + result);
// Less than Operator
result = (x < y);
Console.WriteLine("Less than Operator: " + result);
// Greater than Equal to Operator
result = (x >= y);
Console.WriteLine("Greater than or Equal to: "+ result);
// Less than Equal to Operator
result = (x <= y);
Console.WriteLine("Lesser than or Equal to: "+ result);
// Not Equal To Operator
result = (x != y);
Console.WriteLine("Not Equal to Operator: " + result);
}
}
Output
Addition Operator: 15 Subtraction Operator: 5 Multiplication Operator: 50 Division Operator: 2 Modulo Operator: 0 Equal to Operator: False Greater than Operator: False Less than Operator: True Greater than or Equal to: False Lesser than or Equal to: True Not Equal to Operator: True
Boolean logical operators
Example
// C# program to demonstrate the working
// of Logical and Boolean Operators
using System;
class Interviewbit {
// Main Function
static void Main(string[] args)
{
bool a = true,b = false, result;
// AND operator
result = a && b;
Console.WriteLine("AND Operator: " + result);
// OR operator
result = a || b;
Console.WriteLine("OR Operator: " + result);
// NOT operator
result = !a;
Console.WriteLine("NOT Operator: " + result);
int x = 5, y = 10;
// Bitwise AND Operator
result = x & y;
Console.WriteLine("Bitwise AND: " + result);
// Bitwise OR Operator
result = x | y;
Console.WriteLine("Bitwise OR: " + result);
// Bitwise XOR Operator
result = x ^ y;
Console.WriteLine("Bitwise XOR: " + result);
// Bitwise AND Operator
result = ~x;
Console.WriteLine("Bitwise Complement: " + result);
// Bitwise LEFT SHIFT Operator
result = x << 2;
Console.WriteLine("Bitwise Left Shift: " + result);
// Bitwise RIGHT SHIFT Operator
result = x >> 2;
Console.WriteLine("Bitwise Right Shift: " + result);
}
}
Output:
AND Operator: False OR Operator: True NOT Operator: False Bitwise AND: 0 Bitwise OR: 15 Bitwise XOR: 15 Bitwise Complement: -6 Bitwise Left Shift: 20 Bitwise Right Shift: 1
Task:
You are given two integers as input a and b
You need to perform several task in the editor below:
NOTE: You don’t need to print anything.