Common Operators in C#


  • Assignment operator =
  • Arithmetic operators like +, -, *, /, %
  • Comparison operators like ==, !=, >, >=, <, <=
  • Conditional operators like &&, ||
  • Ternary operator ?:
  • Null coalescing operator ??

Write a program to include all above mentioned common operators to perform different operations.


using System;
class Program
{
    static void Main()
    {
        //use of assignment operator to assign a value to a variable
        int i = 10;
        Console.WriteLine(i);

        //Use of arithmatic operators
        int a = 10;
        int b = 3;
        int sum = a + b;
        int subtraction = a - b;
        int multiply = a * b;
        int divide = a / b;
        int reminder = a % b;
        Console.WriteLine(
            "Addtion:" + sum +
            "\nSubtraction: " + subtraction +
            "\nMultiplication: " + multiply+
            "\nDivision: " + divide +
            "\nReminder: " + reminder
        );

        //Use of comparison operator
        int c = 10;
        if (c == 10) {
            Console.WriteLine("Number is equal to 10");
        }

        //Use of conditional operator
        if (a == 10 && b == 3) {
            Console.WriteLine("Both numners are as expected");
        }

        //use of ternary operator
        bool isNumber = a == 10 ? true : false; 
        Console.WriteLine("a == 10 is {0}", isNumber);        
    }
}

Output:


Comments

Popular posts from this blog

Introduction to C#

Built-in types in C#