C# Print Numbers
Print Numbers
In the Output chapter, you used Console.WriteLine() to print text. You can also use it to print numbers.
Text must be inside double quotes. Numbers are written without quotes:
Example
Console.WriteLine(3);
Console.WriteLine(358);
Console.WriteLine(50000);
Do Math in the Output
You can also do math (calculations) inside WriteLine(). C# calculates the result first, and then prints it:
Example
Console.WriteLine(3 + 3);
The example above prints 6.
Use - to subtract, * to multiply and / to divide:
Example
Console.WriteLine(10 - 4); // 6
Console.WriteLine(2 * 5); // 10
Console.WriteLine(20 / 4); // 5
You will learn more about these math symbols (operators) in the Operators chapter.
Numbers or Text?
Be careful with the double quotes. Without quotes, C# calculates. With quotes, C# sees text, and prints it exactly as you wrote it:
Example
Console.WriteLine(3 + 3); // 6
Console.WriteLine("3 + 3"); // 3 + 3
The first line prints the result of the calculation. The second line prints the text 3 + 3, because it is inside quotes.