Table of Contents

**Operators and Math Functions in C#:**

**Operators and Math Functions in c#:-** A distinction is made between unary and binary operators. The unary operators work with one operand and the binary with two operands. In the statement a = -b that is the minus sign is a unary operator, while with a = b – c that Minus sign is a binary operator. In the following tables both unary and binary operators are listed.

Operators | Description |

+ | Addition |

– | Subtraction or sign (negation) |

* | Multiplication |

/ | Division |

% | Modulo. Remainder of the integer division |

++ | Increment. increase the value by 1 |

— | Decrement. decrease the value by 1 |

+= | Addition and value assignment. a + = b corresponds to a = a + b |

-= | Subtraction and value assignment. a – = b corresponds to a = a – b |

*= | Multiplication and value assignment. a * = b corresponds to a = a * b |

/= | Division and value assignment. a 1 = b corresponds to a = a / b |

%= | Modulo and value assignment. a% = b corresponds to a = a% b |

## Example: how to use simple operators in c#:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo { class Program { static void Main(string[] args) { int a = 20; a += 25; Console.WriteLine(a); a *= 2; Console.WriteLine(a); a -= 90; Console.WriteLine(a); Console .WriteLine(155.0 / 20); Console .WriteLine(155 / 20); Console.WriteLine(155 % 20); Console.ReadKey(); } } } |

### Program explanation:

The variable a has the value 20. 25 is added and the result reassigned to the variable a. The Console.WriteLine(a); shows the result. The next ones run according to the same principle

Operations. The last three lines demonstrate the division with floating point numbers and whole numbers and the modulo operation. 155.0 / 20 gives the result 7.75, which in the German

Version is output as 7.75. 155.0 is a literal of the Data type double. As a result, the 20 that is of type integer is implicitly converted to double, and only then is the division carried out. The result is of type double. 155 /20 returns the result 7, because all operands are of the integer type and all decimal places in the case of an integer division be cut off. With this division, the rest remains 15, which is determined in the last line with the module operation becomes.

**Boolean operators:**

Operators |
Description |

== | equal. Comparison for equality |

!= | Not equal |

> | Greater than |

< | Less than |

>= | Greater than or equal to |

<= | less than or equal to |

&& | Logical and |

|| | Logical or |

! | logical not |

**Bitwise operators:**

Operators | Description |

& | and bitwise |

| | or bitwise |

^ | exclusive or. Bitwise |

>> | Bit shift to the right |

<< | Bit shift to the left |

~ | Complement. bitwise |

## Example: how to use binary and bitwise operators in c#:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo { class Program { static void Main(string[] args) { int a = 20, b = 15; bool c,d; c = true; Console.WriteLine("c ={0}" , c); d = a == b; Console.WriteLine("d = {0}", d); Console.WriteLine ("c or d = {0}", c || d); Console.WriteLine ("c and d = {0}", c & d); a = 8; b = 4; Console.WriteLine(); Console.WriteLine("a bitwise or b ={0}", a | b); Console.WriteLine("a bitwise and b ={0}", a & b); Console.WriteLine("1 shifted 2 bit to the left ={0} ",1<< 2); Console.ReadKey(); } } } |

Output:

**Program explanation:**

The first part of the program deals with logical operations executed with only the values true and false. In of the statement d = a == b, the first equal sign is a Value assignment to d that will be executed after the expression a == b has been evaluated. The comparison a == b results in false, since a and b are different from each other, because c value is true, the OR comparison yields true, while the and comparison yields false. The second part of the program demonstrates bitwise connections, the 8 become dual 1000 and the 4 dual 0100 comparison.

**or comparison:**

1000

0100

——-

1100 corresponds to 12 deciral

**and comparison:**

1000

0100

—–

0000 corresponds to 0 decentralized

The mechanism of bit shifting can be found in the same way, 0001 shifted 2 bits to the left results in 0100, which is the decimal 4 corresponds.

**Math functions in ****C#****:**

math Functions |
Description |

Abs | Absolute value. Abs (a) corresponds to a = a if a> = 0 and a = -a if a <0 |

Sqrt | square root |

Pow | Pow (x,y) corresponds to x^{Y} |

Exp | Exp (x) corresponds to e^{X}. where e is the base of the natural
Is logarithms |

Log | natural logarithm. Logarithm to base e |

Log10 | Logarithm to base 10 |

Sin | Sine |

Asin | Arc sine |

Sinh | Hyperbolic sine |

Cos | Cosine |

Acos | Arc cosine |

Cosh | Cosine hyperbolic |

Tan | tangent |

Atan | Arctangent |

Tanh | Hyperbolic tangent |

Round | Round. the numbers Ibis 4 are rounded down. Above it is rounded up |

Ceiling | Round up |

Floor | Round off |

Max | Max (a,b) is the larger of the values a and b |

Min | Min (a ,b) is the smaller of the values a and b |

Sign | Sign. Sign (a) is I if a> =0 and -1 if a <0 |

**Mathematical constants:**

E e. the bases of the natural logarithms. 2.71828

PI is the ratio of the circumference to the diameter of the circle. 3.14159265

### Example: how to use math functions in c#:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo { class Program { static void Main(string[] args) { double a = Math.Sqrt(3.0*3.0+4.0*4.0); Console.WriteLine("Root of 3 squares plus 4 squares = {0:f2}",a); a = Math.Sin(30.0*Math.PI / 180.0); Console.WriteLine("Sine of 30 degrees = {0:f2}",a ); Console.ReadKey(); } } } |

Output:

The math functions and constants belong to Class Math. Math.Sqrt computes the square root. The payment 3.0 and 4.0 are written with a decimal point. Thereby are they are of type double and match the definition of the function Sqrt. The angle functions expect values in radians. Therefore a conversion with 30.0 * Math.PI / 180.0 must take place, before the sine of 30 degrees can be calculated.