Operator in C#

9
The operator in C#:-

It is used to perform an operation using operand,  operand can be variable, constant, and literals. C# support all operators of C and C++.

c=a+b // here a and b is the variable type operand

10+20  // here 10 and 20 are the literals

Type of Operator:-

1) Unary Operator:-   It will work using a single operand

1.1.1)  Increment|Decrement  :- 

            ++ (increment) ,--(decrement)

           Post  :-     ++,--  a++,a--  (first perform other operation then increment or decrements)

            Pre  :-      ++,--, ++a,--a  (First increment or decrement then perform other operation)


the best example of post and pre-increment and decrement in C# programming languages.




using System;

class Inc
{
    public static void Main()
    {
          int a=2,b;
          b=a++;   //assignment means
          Console.WriteLine("a="+a+" b="+b);
         

    }

}


2) Binary Operator:- 
 It will work using a minimum of Two Operand

2.1.1)   Arithmetic :-    +,-,*,/,%

2.1.2)  Conditional:-    <,>,<=,>=,!=

2.1.3)  Assignment:-    =,+=,-=,*=,/=

2.1.4)  Logical :-     &&, ||, !

2.1.5)  Comparison:-    ==, equals()


3) Ternary Operator:-   It is used to solve the condition-based problem using a single-line statement


var = (condition)  ?    true  :   false;

Q) Example of the ternary operator to calculate a greater number program using two different numbers?

Solution:-

int a=5,b=2;

String s = (a>b)?"a is greater": " b is greater";

Console.WriteLine(s);

Program to check that entered number is an even number or an odd number?

The solution of this program:-

 class TernaryExample
    {
        public static void Main()
        {
            int num;
            Console.WriteLine("Enter number");
            num = int.Parse(Console.ReadLine());
            String s = (num % 2 == 0) ? "Even Number" : "Odd Number";
            Console.WriteLine(s);
            Console.ReadKey();
        }
    }



Assignment:-

1)  WAP to check leap year using ternary operator?

2)  WAP to check that entered char is vowel or consonant?

3)  WAP to calculate the greatest number using three different numbers using the ternary operator?

4)  WAP to calculate the area of the triangle using heron's formula?

5)  WAP to check that number is Armstrong's number or not?


6)  Solve this expression

int a=2,b=5;

b = a++ + a++ + ++a + a++;

b = ++a + a-- + --a + a--;



   
                                  



Tags

Post a Comment

9Comments

POST Answer of Questions and ASK to Doubt

  1. Ans 3):-Calculate the greatest number.

    Console.WriteLine("Enter any Num1 :");
    int a = int.Parse(Console.ReadLine());
    Console.WriteLine("Enter any Num2 :");
    int b = int.Parse(Console.ReadLine());
    Console.WriteLine("Enter any Num3 :");
    int c = int.Parse(Console.ReadLine());
    int R = (a>b && a>c)? a :((b>a && b>c)? b:c);
    Console.Write("Largest number is :{0}",R);
    Console.ReadLine();

    ReplyDelete
  2. Ans 1):- Leap Year or not?

    int y;
    Console.Write("Enter the Year : ");
    y = int.Parse(Console.ReadLine());
    string s =(y%4 == 0 && y%100 != 0)?"It is a Leap Year":"It is not a Leap Year";
    Console.WriteLine("The Year is :{0}",s);
    Console.ReadLine();

    ReplyDelete
  3. Ans 2):-

    {
    char xy;
    Console.WriteLine("Enter Alpha : ");
    xy = char.Parse(Console.ReadLine());
    if (xy =='a'|| xy =='e'|| xy =='i'|| xy =='o'|| xy =='u'|| xy =='A'|| xy =='E'|| xy =='I'|| xy =='O'|| xy =='U')
    {
    Console.WriteLine("Vowel");
    }
    else
    {
    Console.WriteLine("Consonant");
    } Console.ReadLine();

    ReplyDelete
    Replies
    1. using ternary
      char xy;
      Console.WriteLine("Enter Alpha : ");
      xy = char.Parse(Console.ReadLine());
      String s= (xy == 'a' || xy == 'e' || xy == 'i' || xy == 'o' || xy == 'u' || xy == 'A' || xy == 'E' || xy == 'I' || xy == 'O' || xy == 'U')?" Vowel" : " Consonent";
      Console.WriteLine(s);
      Console.ReadLine();

      Delete
  4. Program to calculate area of triangle using herons formula in c#
    class CalulateTriangle
    {
    public static void Main()
    {
    int a = 3000, b = 200, c = 100;
    int s;
    s = (a + b + c) / 2;
    double tarea = Math.Sqrt(s * (s - a) * (s - b) * (s - c));
    Console.WriteLine("Area is {0}", tarea);
    Console.ReadKey();

    }
    }

    ReplyDelete
  5. Solution of Armstrong number in C#?

    class ArmstrongNumber
    {
    static void Main()
    {
    int num = 183,num1;
    num1 = num; //153
    int a = num1 % 10; //3
    num1 = (int)(num1 / 10); //15
    int b = num1 % 10; //5
    int c =(int)(num1 / 10); //1

    int actualnumber = (a * a * a) + (b * b * b) + (c * c * c);
    String s = num == actualnumber ? "armstrong" : "not armstrong";
    Console.WriteLine(s);
    Console.ReadKey();


    }
    }

    ReplyDelete
  6. using System;

    namespace PracticeS
    {
    class Program
    {
    public static void Main()
    {
    int num;
    Console.WriteLine("Enter Year :");
    num = Convert.ToInt32(Console.ReadLine());
    string s = (num%4==0) ? "this is a leap year" : "this is a not leap Year";
    Console.WriteLine(s);
    Console.ReadLine();
    }
    }
    }

    ReplyDelete
  7. class Program
    {
    public static void Main()
    {
    int num1,num2;
    Console.WriteLine("Enter Number 1 :");
    num1 = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("Enter Number2 :");
    num2 = Convert.ToInt32(Console.ReadLine());
    string s =(num1>num2)? "Number 1 is Greater" : "Number 2 is Greater";
    Console.WriteLine(s);
    Console.ReadLine();
    }
    }

    ReplyDelete
  8. 6) A--> class Program
    {
    public static void Main()
    {
    int a = 2, b = 5;
    b = a++ + a++ + ++a + a++;
    Console.WriteLine(a);
    Console.WriteLine(b);
    Console.ReadLine();
    }
    }
    }


    6) B-->
    class Program
    {
    public static void Main()
    {
    int a = 2, b = 5;
    b = b = ++a + a-- + --a + a--;
    Console.WriteLine(a);
    Console.WriteLine(b);
    Console.ReadLine();
    }
    }
    }

    ReplyDelete
Post a Comment