التخطي إلى المحتوى الرئيسي

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



   
                                  



تعليقات

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

    ردحذف
  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();

    ردحذف
  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();

    ردحذف
    الردود
    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();

      حذف
  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();

    }
    }

    ردحذف
  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();


    }
    }

    ردحذف
  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();
    }
    }
    }

    ردحذف
  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();
    }
    }

    ردحذف
  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();
    }
    }
    }

    ردحذف
  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();
    }

    ردحذف

إرسال تعليق

POST Answer of Questions and ASK to Doubt

المشاركات الشائعة من هذه المدونة

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

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

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...