VARIABLES. OUTPUT FORMATS


Variables
A variable is a cell in computer memory that has a name and stores some value corresponding to the type .

The word "variable" tells us that its value can change during program execution.  When a new variable value is saved, the old one is erased.


For a computer, all information is data in its memory - sets of zeros and ones (to put it simply, any information in a computer is just numbers, and it processes them in the same way). However, we know that integers and fractional numbers work differently. Therefore, each programming language has different types of data, which are processed using different methods.

For example,
- integer variables – type int (from English integer – whole), occupy 4 bytes in memory;
- real variables that can have a fractional part (type float – from English floating point – floating point) occupy 4 bytes in memory;
- characters (type char – from English character – symbol), occupy 1 byte in memory.

Let's try to add some variable to our program.
Before using a variable, you need to tell the computer to allocate space in memory for it. To do this, you need to declare a variable, that is, specify what type of value it will store, and give it a name.
Also, if necessary, you can assign initial values ​​to it. 

Let's take a program as an example:
using System;
class Program {
    static void Main()
    {
        int a = 6, b; // declared two variables of integer type, in variable a immediately saved the value 6.
  // Variable b was not initialized;
                      // what will be in memory in this case, we do not know.
    }
}


A computer would not be needed if it did not have the ability to store various information in its memory and be able to process information of the same type using the same algorithms. 
In order to create more interesting programs, one must learn how to store information in the computer's memory. At the same time, we need to learn how to somehow access the memory cells of the computer. 
In programming, as in life, in order to refer to any part of the computer's memory, it occurs by name. Using this name, you can both read information and write it there.

Example
Let's try to write a calculator for prime numbers.

Our task is to display some arithmetic expression on the screen and make the computer calculate it.
For example: 
5+7=12
Moreover, instead of 5 and 7, there can be different numbers, depending on the values ​​of the variables a and b in the program.

In the output statement, you can display not just text, but also the values ​​of variables, as well as the result of an arithmetic expression. Moreover, the output sequence may be different. For example, in order to display the above expression, you need to write it like this:
Console.WriteLine(a + "+" + b + "=" + (a+b));
If we want to display the value of a variable, then we just need to specify its name without quotes. If we want to display the result of an arithmetic expression, then it is enough to simply write the arithmetic expression correctly. Please note: 
Variables and text are separated from each other by the "+" operator, while the text is written in quotes, and variables without them.
 

Enter statement
In order for the user to be able to set the value of the variable himself, it is necessary to be able to enter values ​​from the keyboard. 
C# has two value input operators: Console.Read(); and Console.ReadLine();
 
Read reads only one character from the entered values, or -1 if there are no more characters left to read. Moreover, the method returns an integer character code, so to get a character variable, you need to perform a conversion using the Convert.ToChar().
 
int x = Console.Read(); // read character code
char a = Convert.ToChar(x); // converting the received code into the value of a character variable
 
With ReadLine() , you can read a string sequence before entering a new line. As a result, the method may return a string or null if there are no more strings.

For example, the entry reads the line:
stringline = Console.ReadLine();

To read an integer value, you need to read the string and convert it to a number:
 
int a = int.Parse(Console.ReadLine());
 
If the numbers go in a line, then you need to count the line, & nbsp; and get an array of strings from it using the space character as a separator. And then each element of the array is converted to a number:
string[] numbers = Console.ReadLine().Split(' ');
int a = int Parse(numbers[0]);
int b = int.Parse(numbers[1]);

Output Specifiers
To output real values, just call the Console.Write or Console.WriteLine:
method   double a = 0.9999; Console.Write(a);  
But sometimes you need to pre-format the output of values, it is convenient to do this using the String.Format method, more details here.
Output with a certain precision
For formatting fractional numbers, the f specifier is used, the number after which indicates how many characters will be after the separator. double a = 12.123; Console.WriteLine(String.Format("{0:f2}", a)); The result will be 12,12, but if you use the value 0.9999, uncontrolled rounding will occur and   1.00.

Therefore, the following algorithm is used to discard signs rather than rounding:
1) multiply the original value by 10, as many times as you need to leave decimal places;
2) using the   Math.Truncate method, we leave only the integer part;
3) divide the resulting value by 10, as many times as you need to leave decimal places.

Example for output with two decimal precision: 
double a = 0.9999; a = a * Math.Pow(10, 2); a = Math.Truncate(a); a = a / Math.Pow(10, 2);
 
The type of separator when outputting the result (dot or comma) depends on the regional settings of the computer, so to always use a period as a separator, you need to change the regional settings to invariant, resulting example:
 
CultureInfo ci = new CultureInfo(""); double a = 0.9999; a = a * Math.Pow(10, 2); a = Math.Truncate(a); a = a / Math.Pow(10, 2); Console.WriteLine(a.ToString(ci));