Operators and Math Functions in C# with Examples

(Last Updated On: April 9, 2021)

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#:

Math Functions in c#

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#:



Math Functions in c#

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:



1100 corresponds to 12 deciral


and comparison:


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 xY
Exp Exp (x) corresponds to eX. 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#:



Math Functions in c#

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.



Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply