Last Updated:

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:

  1. Addition (+);
  2. Subtraction (-);
  3. Multiplication (*);
  4. 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.


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

We beautifully designed the output, and also placed the calculation in the output function itself.
A little explanation:

  1. The plus sign (+) in the output function allows you to combine several character values into one, and thus get the most beautifully designed output;
  2. 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;
  3. 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;

That's our fractional part in the calculation.

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