Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Operators in C#

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:

  • Arithmetic Operators that perform arithmetic operations with numeric operands
  • Comparison operators that compare numeric operands
  • Boolean logical operators that perform logical operations with bool operands
  • Bitwise and shift operators that perform bitwise or shift operations with operands of the integral types
  • Equality operators that check if their operands are equal or not

In C#, Operators can also categorized based upon Number of Operands :

  • Unary Operator: Operator that takes one operand to perform the operation.
  • Binary Operator: Operator that takes two operands to perform the operation.
  • Ternary Operator: Operator that takes three operands to perform the operation.

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:

  • Increment: The ‘++’ operator is used to increment the value of an integer. When placed before the variable name (also called pre-increment operator), its value is incremented instantly. For example, ++x.
    And when it is placed after the variable name (also called post-increment operator), its value is preserved temporarily until the execution of this statement and it gets updated before the execution of the next statement. For example, x++.
  • Decrement: The ‘--‘ operator is used to decrement the value of an integer. When placed before the variable name (also called pre-decrement operator), its value is decremented instantly. For example, --x.
    And when it is placed after the variable name (also called post-decrement operator), its value is preserved temporarily until the execution of this statement and it gets updated before the execution of the next statement. For example, x--.

Comparison Operators

Comparison operators are used for comparison of two values. Let’s see them one by one:

  • ‘==`(Equal To) operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise it returns false. For example, 5==5 will return true.
  • ‘!=`(Not Equal To) operator checks whether the two given operands are equal or not. If not, it returns true. Otherwise it returns false. It is the exact boolean complement of the ‘==’ operator. For example, 5!=5 will return false.
  • ‘>`(Greater Than) operator checks whether the first operand is greater than the second operand. If so, it returns true. Otherwise it returns false. For example, 6>5 will return true.
  • ‘<‘(Less Than) operator checks whether the first operand is lesser than the second operand. If so, it returns true. Otherwise it returns false. For example, 6<5 will return false.
  • ‘>='(Greater Than Equal To) operator checks whether the first operand is greater than or equal to the second operand. If so, it returns true. Otherwise it returns false. For example, 5>=5 will return true.
  • ‘<='(Less Than Equal To) operator checks whether the first operand is lesser than or equal to the second operand. If so, it returns true. Otherwise it returns false. For example, 5<=5 will also return true.

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

  • Logical AND: The ‘&&’ operator returns true when both the conditions in consideration are satisfied. Otherwise it returns false. For example, a && b returns true when both a and b are true (i.e. non-zero).
  • Logical OR: The ‘||’ operator returns true when one (or both) of the conditions in consideration is satisfied. Otherwise it returns false. For example, a || b returns true if one of a or b is true (i.e. non-zero). Of course, it returns true when both a and b are true.
  • Logical NOT: The ‘!’ operator returns true the condition in consideration is not satisfied. Otherwise it returns false. For example, !a returns true if a is false, i.e. when a=0.
  • & (bitwise AND) Takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.
  • | (bitwise OR) Takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 any of the two bits is 1.
  • ^ (bitwise XOR) Takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.
  • << (left shift) Takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.
  • >> (right shift) Takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.

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:

  • In the variable named "add" store the sum of a and b.
  • In the variable named "sub" store the difference of a with b.
  • In the variable named "multi" store the multiplication of a and b.
  • In the variable named "div" store the division of a by b.

NOTE: You don’t need to print anything.

Start solving Operators in C# on Interview Code Editor
Hints
  • Hints are not available for this problem

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test