Skip to main content

Operator in C#

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--;



   
                                  



Comments

  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
  9. static void Main(string[] args)
    {
    //float a=3, b=6, c=7;
    //float semipermiter = (a + b + c) / 2;
    //float area = ((float)Math.Sqrt(semipermiter * (semipermiter-a) * (semipermiter-b) *( semipermiter-c)));
    //Console.WriteLine("area of triangle is :"+area);
    Console.WriteLine("Enter a year :");
    int year =int.Parse(Console.ReadLine());
    String s = (year % 4 == 0 || year % 400 == 0) ? "Leap Year" : "Not Leap Year";
    Console.WriteLine(s);
    Console.ReadKey();
    }

    ReplyDelete

Post a Comment

POST Answer of Questions and ASK to Doubt

Popular posts from this blog

DSA in C# | Data Structure and Algorithm using C#

  DSA in C# |  Data Structure and Algorithm using C#: Lecture 1: Introduction to Data Structures and Algorithms (1 Hour) 1.1 What are Data Structures? Data Structures are ways to store and organize data so it can be used efficiently. Think of data structures as containers that hold data in a specific format. Types of Data Structures: Primitive Data Structures : These are basic structures built into the language. Example: int , float , char , bool in C#. Example : csharp int age = 25;  // 'age' stores an integer value. bool isStudent = true;  // 'isStudent' stores a boolean value. Non-Primitive Data Structures : These are more complex and are built using primitive types. They are divided into: Linear : Arrays, Lists, Queues, Stacks (data is arranged in a sequence). Non-Linear : Trees, Graphs (data is connected in more complex ways). Example : // Array is a simple linear data structure int[] number...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

Top 50 Most Asked MERN Stack Interview Questions and Answers for 2025

 Top 50 Most Asked MERN Stack Interview Questions and Answers for 2025 Now a days most of the IT Company asked NODE JS Question mostly in interview. I am creating this article to provide help to all MERN Stack developer , who is in doubt that which type of question can be asked in MERN Stack  then they can learn from this article. I am Shiva Gautam,  I have 15 Years of experience in Multiple IT Technology, I am Founder of Shiva Concept Solution Best Programming Institute with 100% Job placement guarantee. for more information visit  Shiva Concept Solution 1. What is the MERN Stack? Answer : MERN Stack is a full-stack JavaScript framework using MongoDB (database), Express.js (backend framework), React (frontend library), and Node.js (server runtime). It’s popular for building fast, scalable web apps with one language—JavaScript. 2. What is MongoDB, and why use it in MERN? Answer : MongoDB is a NoSQL database that stores data in flexible, JSON-like documents. It...