# Mathematics and Operations in C# (sharp)

All programming, in general, is built on mathematics. If there were no mathematics, there would be no programming. The very essence of programming is to simplify the mathematical problem **as much as possible** and** solve it very quickly**. Modern computers have very **powerful hardware** than computers did a decade ago. If, literally, half a century ago, great minds **dreamed and dreamed** of one gigabyte of memory, then in our time the calculation of memory goes in terabytes.

If on ordinary user (ours) computers a hard disk costs **an average of 1 terabyte** of memory, then in the scientific community memory **is calculated in tens, or even hundreds, terabytes**. Computing power allows you** to build and design** similarities **of artificial intelligence** capable of solving specific problems, as well as simulate realistic **3D models** of any complexity. Another ten years and graphics in computer games can be compared with high-quality photos of professional photographers.

## Classic operations

Actually, each programming language has four basic, classical mathematical operators:

- Addition (+);
- Subtraction (-);
- Multiplication (*);
- Division (/);

There are only four operators, and the combinations and schemes cranked with them are **not countable**!

Also, in addition to the main ones, there are **additional mathematical operators** specifically of the C# language. For example, finding **the remainder of division** (%). This is where the additional operators end. Yes, there is only one.

## Implementation with type int

Let's consider an example of how to calculate and display each result on the screen at once.

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 | using System; //linked library using System.Collections.Generic; //linked library using System.Linq; //linked library using System.Text; //linked library using System.Threading.Tasks; //linked library namespace ConsoleApplication1 // namespace connection { class Program // class { static void Main(string[] args) // class method { Console.WriteLine("Math Operations"); //output text without wrapping the line on the screen int a = 10, b = 3; //create two integer variables with values Console.WriteLine(a + " + " + b + " = " + (a + b)); //addition of two numbers Console.WriteLine(a + " - " + b + " = " + (a - b)); //subtraction of two numbers Console.WriteLine(a + " * " + b + " = " + (a * b)); //multiply two numbers Console.WriteLine(a + " / " + b + " = " + (a / b)); // division of two numbers Console.WriteLine(a + " % " + b + " = " + (a % b)); // find the remainder Console.Read(); // compiler wait for keyboard input and press Enter } } } |

1 2 3 4 5 6 | Mathematical operations 10 + 3 = 13 10 - 3 = 7 10 * 3 = 30 10 / 3 = 3 10 % 3 = 1 |

As you can see, during the division, the incomprehensible happened. There should also be a fractional part. This is a "**feature" of the** language, if **integer variables** are divided (as in our case), then only **the whole part of the division** is displayed on the screen, that is, what is before the "comma". In order to divide in the usual way, it is necessary that at least **one of the operands** (elements of calculation) belong to **the floating-point type**.

Implement the same example, only one of the variables will be of type float.

## Implementation with float type

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 | using System; //linked library using System.Collections.Generic; //linked library using System.Linq; //linked library using System.Text; //linked library using System.Threading.Tasks; //linked library namespace ConsoleApplication1 // namespace connection { class Program // class { static void Main(string[] args) // class method { Console.WriteLine("Math Operations"); //output text without wrapping the line on the screen int a = 10; //create an integer variable with a value float b = 3f; //create a floating point variable with a value Console.WriteLine(a + " + " + b + " = " + (a + b)); //addition of two numbers Console.WriteLine(a + " - " + b + " = " + (a - b)); //subtraction of two numbers Console.WriteLine(a + " * " + b + " = " + (a * b)); //multiply two numbers Console.WriteLine(a + " / " + b + " = " + (a / b)); // division of two numbers Console.WriteLine(a + " % " + b + " = " + (a % b)); // find the remainder Console.Read(); // compiler wait for keyboard input and press Enter } } } |

We beautifully designed the output, and also placed the calculation in the output function itself.

A little explanation:

- The plus sign (+) in the output function allows you
**to combine several character values**into one, and thus get**the most beautifully designed**output; - Everything in the parentheses of the output function is recognized by the function
**as symbols**, that is, any variable (no matter numeric or symbolic) is converted**into a set of characters**. But, if only two variables**of a numeric type**combined with a**plus sign**(+) are indicated in parentheses, then a mathematical operation of addition between numbers occurs and the resulting result is displayed as a set of characters; - for the function to understand that it is necessary to perform a mathematical
**operation on a group of operands**they must be**separated from the rest of the output values****with parentheses**;

1 2 3 4 5 6 | Mathematical operations 10 + 3 = 13 10 - 3 = 7 10 * 3 = 30 10 / 3 = 3,333333 10 % 3 = 1 |

That's our fractional part in the calculation.

Expressions can be incredibly long, or they can be short (as shown in the example).