Saturday, 2 July 2016

Operators

In C#, you can use the following types of operators:
Arithmetic operators
v  The commonly used arithmetic operators are:
+     Used to add two numbers.
-      Used to subtract two numbers.
*     Used to multiply two numbers.
/      Used to divide one number by another.
%    Used to divide two numbers and return the remainder.
Example
using System;

namespace Arithmetic_Operators
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1, num2;
            int add, sub, mul;
            float div;

            //Accepting Values from users
            Console.Write("Enter first number\t\t");
            num1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("\n\nEnter second number\t\t");
            num2 = Convert.ToInt32(Console.ReadLine());

            //Processing Values
            //Used + operator for adding values
            add = num1 + num2;
            //Used - operator for subtracting values
            sub = num1 - num2;
            //Used * operator for multiplying values
            mul = num1 * num2;
            //Used / operator for dividing values
            div = (float)num1 / num2;

            //Displaying Output
            Console.WriteLine("\n\n=====================\n");
            Console.WriteLine("Addition\t\t{0}", add);
            Console.WriteLine("Subtraction\t\t{0}", sub);
            Console.WriteLine("Multiplication\t\t{0}", mul);
            Console.WriteLine("Division\t\t{0}", div);
            Console.WriteLine("\n=======================\n");

            Console.ReadLine();
        }
    }
}


Arithmetic assignment operators
v  The commonly used arithmetic assignment operators are:
+=   For example: X+=Y; can also be interpreted as X = X + Y;.
-=    For example: X-=Y; can be interpreted as X = X – Y;.
*=   For example: X*=Y; can be interpreted as X = X * Y;.
/=   For example: X/=Y; can be interpreted as X = X / Y;.
%=  For example: X%=Y; can be interpreted as X = X % Y;.
Example
using System;
 
namespace assignment_operator
{
  class Program
   {
     static void Main(string[] args)
      {
        int num1,num2;
 
        num1 = 10;
        num2 = 5;
 
        num1 += num2; // same as num1=num1+num2
        Console.WriteLine("Add = {0}", num1);
 
        num1 -= num2; // same as num1=num1-num2
        Console.WriteLine("\n\nSubtraction = {0}", num1);
 
        num1 *= num2; // same as num1=num1*num2
        Console.WriteLine("\n\nMultiplication={0}",num1);
 
        num1 %= num2; // same as num1=num1%num2
        Console.WriteLine("\n\nModulus = {0}", num1);
 
        Console.ReadLine();
      }
   }
}

Unary operators
v  The commonly used increment and decrement operators are:
++(Pre/Post increment operator): Used to increment the value of an operand by 1, pre or post assignment.
--(Pre/Post decrement operator): Used to decrement the value of an operand by 1, pre or post assignment.
Example
using System;
 
namespace Increment_Operator
{
  class Program
   {
     static void Main(string[] args)
      {
        int i = 0; // initialization
 
        i++; // i incremented by one. It is post increment
 
        Console.WriteLine("The value of i is {0}", i);
 
        Console.ReadLine();
      }
   }
}

Comparison operators
v  Some commonly used comparison operators are:
< : Used to check whether expression1 is less than expression2.
> : Used to check whether expression1 is greater than expression2.
<= : Used to check whether expression1 is less than or equal to expression2.
>= : Used to check whether expression1 is greater than or equal to expression2.
== : Used to check whether expression1 is equal to expression2.
!= : Used to check whether expression1 is not equal to expression2.
Example
using System;
 
namespace Comparison_Operator
{
  class Program
   {
     static void Main(string[] args)
      {
        int num1, num2;
 
        //Accepting two inputs from the user
        Console.Write("Enter first number\t");
        num1 = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter second number\t");
        num2 = Convert.ToInt32(Console.ReadLine());
 
        //Processing comparison
        //Check whether num1 is greater than or not
        if (num1 > num2)
         {
           Console.WriteLine("{0} is greater than {1}", num1, num2);
         }
        //Check whether num2 is greater than or not
        else if (num2 > num1)
         {
           Console.WriteLine("{0} is greater than {1}",num2, num1);
         }
        else
         {
           Console.WriteLine("{0} and {1} are equal",num1, num2);
         
        Console.ReadLine();          
      }
   }
}

Logical operators
v  Some commonly used logical operators are:
&& : Returns true if both, expression1 and expression2, are true.
! : Returns true if the expression is false.
|| : Returns true if either expression1 or expression2 or both of them are true.
^ : Returns true if either expression1 or expression2 is true. It returns false if both expression1 and expression2 are true or if both expression1 and expression2 are false.
Example
using System;

namespace Or_operator
{
    class Program
    {
        static void Main(string[] args)
        {
            string uname, pwd;

        label: //Creating label

            Console.Write("\n\nEnter your login name:\t");
            uname = Console.ReadLine();

            Console.Write("Enter your password:\t");
            pwd = Console.ReadLine();

            if ((uname=="Kumar" || uname=="Amit")&&(pwd=="Kumaramit"))
            {
                Console.WriteLine("\nLogin Successful.");
            }
            else
            {
               Console.WriteLine("\nUnauthorised Access.Aborting...");
            }

            Console.Write("\n\nPress Y or y for continue.:\t");
            char ans = Convert.ToChar(Console.ReadLine());
            if (ans=='Y' || ans=='y')
            {
                goto label; //goto label
            }
            Console.WriteLine("Press  Enter for Aborting...");
            Console.ReadLine();
        }
    }
}




No comments:

Post a Comment