Skip to content

Commit fcc9f12

Browse files
committed
First pass at the numbers tutorial
1 parent 3f9d154 commit fcc9f12

File tree

3 files changed

+174
-69
lines changed

3 files changed

+174
-69
lines changed

docs/csharp/tour-of-csharp/tutorials/numbers-in-csharp.md

Lines changed: 130 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,165 @@
11
---
2-
title: Work with Numbers - Introductory interactive tutorial
3-
description: In this tutorial about numeric types, you use your browser to learn C# interactively. You're going to write C# code and see the results of compiling and running your code directly in the browser.
4-
ms.date: 03/06/2025
2+
title: Work with Numbers - Introductory tutorial
3+
description: This tutorial teaches you about the numeric types in C#. The tutorial contains a series of lessons that explore numbers and math operations in C#.
4+
ms.date: 12/02/2025
55
---
66
# How to use integer and floating point numbers in C\#
77

88
This tutorial teaches you about the numeric types in C#. You write small amounts of code, then you compile and run that code. The tutorial contains a series of lessons that explore numbers and math operations in C#. These lessons teach you the fundamentals of the C# language.
99

10-
> [!TIP]
11-
>
12-
> When a code snippet block includes the "Run" button, that button opens the interactive window, or replaces the existing code in the interactive window. When the snippet doesn't include a "Run" button, you can copy the code and add it to the current interactive window.
10+
## Explore integer math
1311

1412
## Explore integer math
1513

16-
Run the following code in the interactive window.
14+
Create a directory named *numbers-quickstart*. Make it the current directory and run the following command:
15+
16+
```dotnetcli
17+
dotnet new console -n NumbersInCSharp -o .
18+
```
19+
20+
Open *Program.cs* in your favorite editor, and replace the contents of the file with the following code:
1721

18-
:::code language="csharp" interactive="try-dotnet-method" source="./snippets/NumbersInCsharp/Program.cs" id="Addition":::
22+
:::code language="csharp" source="./snippets/NumbersInCsharp/Program.cs" id="Addition":::
23+
Addition
1924

20-
The preceding code demonstrates fundamental math operations with integers. The `int` type represents an **integer**, a positive or negative whole number. You use the `+` symbol for addition. Other common mathematical operations for integers include:
25+
Run this code by typing `dotnet run` in your command window.
26+
27+
You've seen one of the fundamental math operations with integers. The `int` type represents an **integer**, a zero, positive, or negative whole number. You use the `+` symbol for addition. Other common mathematical operations for integers include:
2128

2229
- `-` for subtraction
2330
- `*` for multiplication
2431
- `/` for division
2532

26-
Start by exploring those different operations. Modify the third line to try each of these operations. For example, to try subtraction, replace the `+` with a `-` as shown in the following line:
33+
Start by exploring those different operations. Add these lines after the line that writes the value of `c`:
2734

28-
```csharp
29-
int c = a - b;
30-
```
35+
:::code language="csharp" source="./snippets/NumbersInCsharp/Program.cs" id="OtherOperations":::
36+
37+
Run this code by typing `dotnet run` in your command window.
3138

32-
Try it. Select the "Run" button. Then, try multiplication, `*` and, division, `/`. You can also experiment by writing multiple mathematics operations in the same line, if you'd like.
39+
You can also experiment by writing multiple mathematics operations in the same line, if you'd like. Try `c = a + b - 12 * 17;` for example. Mixing variables and constant numbers is allowed.
3340

3441
> [!TIP]
35-
>
36-
> As you explore C# (or any programming language), you make mistakes when you write code. The **compiler** finds those errors and report them to you. When the output contains error messages, look closely at the example code, and the code in the interactive window to see what to fix. That exercise helps you learn the structure of C# code.
42+
> As you explore C# (or any programming language), you'll make mistakes when you write code. The **compiler** will find those errors and report them to you. When the output contains error messages, look closely at the example code and the code in your window to see what to fix. That exercise will help you learn the structure of C# code.
43+
44+
You've finished the first step. Before you start the next section, let's move the current code into a separate *method*. A method is a series of statements grouped together and given a name. You call a method by writing the method's name followed by `()`. Organizing your code into methods makes it easier to start working with a new example. When you finish, your code should look like this:
45+
46+
```csharp
47+
WorkWithIntegers();
48+
49+
void WorkWithIntegers()
50+
{
51+
int a = 18;
52+
int b = 6;
53+
int c = a + b;
54+
Console.WriteLine(c);
55+
56+
57+
// subtraction
58+
c = a - b;
59+
Console.WriteLine(c);
60+
61+
// multiplication
62+
c = a * b;
63+
Console.WriteLine(c);
64+
65+
// division
66+
c = a / b;
67+
Console.WriteLine(c);
68+
}
69+
```
3770

3871
## Explore order of operations
3972

40-
The C# language defines the precedence of different mathematics operations with rules consistent with the rules you learned in mathematics. Multiplication and division take precedence over addition and subtraction. Explore that by running the following code in the interactive window:
73+
Comment out the call to `WorkingWithIntegers()`. It will make the output less cluttered as you work in this section:
74+
75+
```csharp
76+
//WorkWithIntegers();
77+
```
78+
79+
The `//` starts a **comment** in C#. Comments are any text you want to keep in your source code but not execute as code. The compiler doesn't generate any executable code from comments. Because `WorkWithIntegers()` is a method, you need to only comment out one line.
80+
81+
The C# language defines the precedence of different mathematics operations with rules consistent with the rules you learned in mathematics. Multiplication and division take precedence over addition and subtraction. Explore that by adding the following code after the call to `WorkWithIntegers()`, and executing `dotnet run`:
4182

42-
:::code language="csharp" interactive="try-dotnet-method" source="./snippets/NumbersInCsharp/Program.cs" id="Precedence":::
83+
:::code language="csharp" source="./snippets/NumbersInCsharp/Program.cs" id="Precedence":::
4384

4485
The output demonstrates that the multiplication is performed before the addition.
4586

46-
You can force a different order of operation by adding parentheses around the operation or operations you want performed first. Add the following lines to the interactive window:
87+
You can force a different order of operation by adding parentheses around the operation or operations you want performed first. Add the following lines and run again:
4788

4889
:::code language="csharp" source="./snippets/NumbersInCsharp/Program.cs" id="Parentheses":::
4990

50-
Explore more by combining many different operations. Replace the fourth line in the preceding code with something like this:
91+
Explore more by combining many different operations. Add something like the following lines. Try `dotnet run` again.
5192

5293
:::code language="csharp" source="./snippets/NumbersInCsharp/Program.cs" id="CompoundExpression":::
5394

54-
You might notice an interesting behavior for integers. Integer division always produces an integer result, even when you'd expect the result to include a decimal or fractional portion.
95+
You may have noticed an interesting behavior for integers. Integer division always produces an integer result, even when you'd expect the result to include a decimal or fractional portion.
96+
97+
If you haven't seen this behavior, try the following code:
98+
99+
:::code language="csharp" source="./snippets/NumbersInCsharp/Program.cs" id="Truncation":::
55100

56-
Try the following code:
101+
Type `dotnet run` again to see the results.
57102

58-
:::code language="csharp" interactive="try-dotnet-method" source="./snippets/NumbersInCsharp/Program.cs" id="Truncation":::
103+
Before moving on, let's take all the code you've written in this section and put it in a new method. Call that new method `OrderPrecedence`. Your code should look something like this:
104+
105+
```csharp
106+
// WorkWithIntegers();
107+
OrderPrecedence();
108+
109+
void WorkWithIntegers()
110+
{
111+
int a = 18;
112+
int b = 6;
113+
int c = a + b;
114+
Console.WriteLine(c);
115+
116+
117+
// subtraction
118+
c = a - b;
119+
Console.WriteLine(c);
120+
121+
// multiplication
122+
c = a * b;
123+
Console.WriteLine(c);
124+
125+
// division
126+
c = a / b;
127+
Console.WriteLine(c);
128+
}
129+
130+
void OrderPrecedence()
131+
{
132+
int a = 5;
133+
int b = 4;
134+
int c = 2;
135+
int d = a + b * c;
136+
Console.WriteLine(d);
137+
138+
d = (a + b) * c;
139+
Console.WriteLine(d);
140+
141+
d = (a + b) - 6 * c + (12 * 4) / 3 + 12;
142+
Console.WriteLine(d);
143+
144+
int e = 7;
145+
int f = 4;
146+
int g = 3;
147+
int h = (e + f) / g;
148+
Console.WriteLine(h);
149+
}
150+
```
59151

60152
## Explore integer precision and limits
61153

62-
That last sample showed you that integer division truncates the result. You can get the **remainder** by using the **remainder** operator, the `%` character:
154+
That last sample showed you that integer division truncates the result. You can get the **remainder** by using the **remaimnder** operator, the `%` character. Try the following code after the method call to `OrderPrecedence()`:
63155

64-
:::code language="csharp" interactive="try-dotnet-method" source="./snippets/NumbersInCsharp/Program.cs" id="QuotientAndRemainder":::
156+
:::code language="csharp" source="./snippets/NumbersInCsharp/Program.cs" id="QuotientAndRemainder":::
65157

66158
The C# integer type differs from mathematical integers in one other way: the `int` type has minimum and maximum limits. Try the following code to see those limits:
67159

68160
:::code language="csharp" source="./snippets/NumbersInCsharp/Program.cs" id="MinAndMax":::
69161

70-
If a calculation produces a value that exceeds those limits, you have an **underflow** or **overflow** condition. The answer appears to wrap from one limit to the other. To see an example, add these two lines in the interactive window:
162+
If a calculation produces a value that exceeds those limits, you have an **underflow** or **overflow** condition. The answer appears to wrap from one limit to the other. To see an example, add these two lines to your code:
71163

72164
:::code language="csharp" source="./snippets/NumbersInCsharp/Program.cs" id="Overflow":::
73165

@@ -77,35 +169,35 @@ There are other numeric types with different limits and precision that you would
77169

78170
## Work with the double type
79171

80-
The `double` numeric type represents a double-precision floating point number. Those terms might be new to you. A **floating point** number is useful to represent nonintegral numbers that might be large or small in magnitude. **Double-precision** is a relative term that describes the number of binary digits used to store the value. **Double precision** numbers have twice the number of binary digits as **single-precision**. On modern computers, it's more common to use double precision than single precision numbers. **Single precision** numbers are declared using the `float` keyword. Let's explore. Run the following code and see the result:
172+
The `double` numeric type represents a double-precision floating point number. Those terms may be new to you. A **floating point** number is useful to represent non-integral numbers that may be very large or small in magnitude. **Double-precision** is a relative term that describes the number of binary digits used to store the value. **Double precision** numbers have twice the number of binary digits as **single-precision**. On modern computers, it's more common to use double precision than single precision numbers. **Single precision** numbers are declared using the `float` keyword. Let's explore. Add the following code and see the result:
81173

82-
:::code language="csharp" interactive="try-dotnet-method" source="./snippets/NumbersInCsharp/Program.cs" id="FloatingPoint":::
174+
:::code language="csharp" source="./snippets/NumbersInCsharp/Program.cs" id="FloatingPoint":::
83175

84176
Notice that the answer includes the decimal portion of the quotient. Try a slightly more complicated expression with doubles. You can use the following values, or substitute other numbers:
85177

86178
:::code language="csharp" source="./snippets/NumbersInCsharp/Program.cs" id="ChangeDoubleValues":::
87179

88-
The range of a double value is greater than integer values. Try the following code in the interactive window:
180+
The range of a double value is much greater than integer values. Try the following code below what you've written so far:
89181

90182
:::code language="csharp" source="./snippets/NumbersInCsharp/Program.cs" id="MinMax":::
91183

92-
These values are printed in scientific notation. The number before the `E` is the significand. The number after the `E` is the exponent, as a power of 10.
93-
94-
Just like decimal numbers in math, doubles in C# can have rounding errors. Try this code:
184+
These values are printed in scientific notation. The number to the left of the `E` is the significand. The number to the right is the exponent, as a power of 10. Just like decimal numbers in math, doubles in C# can have rounding errors. Try this code:
95185

96186
:::code language="csharp" source="./snippets/NumbersInCsharp/Program.cs" id="RoundingError":::
97187

98188
You know that `0.3` is `3/10` and not exactly the same as `1/3`. Similarly, `0.33` is `33/100`. That value is closer to `1/3`, but still not exact. No matter how many decimal places you add, a rounding error remains.
99189

100190
***Challenge***
101191

102-
Try other calculations with large numbers, small numbers, multiplication, and division using the `double` type. Try more complicated calculations.
192+
Try other calculations with large numbers, small numbers, multiplication, and division using the `double` type. Try more complicated calculations. After you've spent some time with the challenge, take the code you've written and place it in a new method. Name that new method `WorkWithDoubles`.
103193

104194
## Work with decimal types
105195

106-
There's one other type to learn: the `decimal` type. The `decimal` type has a smaller range but greater precision than `double`. Let's take a look:
196+
You've seen the basic numeric types in C#: integers and doubles. There's one other type to learn: the `decimal` type. The `decimal` type has a smaller range but greater precision than `double`. Let's take a look:
197+
198+
:::code language="csharp" source="./snippets/NumbersInCsharp/Program.cs" id="Decimal":::
107199

108-
:::code language="csharp" interactive="try-dotnet-method" source="./snippets/NumbersInCsharp/Program.cs" id="Decimal":::
200+
There's one other type to learn: the `decimal` type. The `decimal` type has a smaller range but greater precision than `double`. Let's take a look:
109201

110202
Notice that the range is smaller than the `double` type. You can see the greater precision with the decimal type by trying the following code:
111203

@@ -117,10 +209,11 @@ The `M` suffix on the numbers is how you indicate that a constant should use the
117209

118210
> [!NOTE]
119211
> The letter `M` was chosen as the most visually distinct letter between the `double` and `decimal` keywords.
212+
Notice that the math using the decimal type has more digits to the right of the decimal point.
120213

121214
***Challenge***
122215

123-
Write code that calculates the area of a circle whose radius is 2.50 centimeters. Remember that the area of a circle is the radius squared multiplied by PI. One hint: .NET contains a constant for PI, <xref:System.Math.PI?displayProperty=nameWithType> that you can use for that value. <xref:System.Math.PI?displayProperty=nameWithType>, like all constants declared in the `System.Math` namespace, is a `double` value. For that reason, you should use `double` instead of `decimal` values for this challenge.
216+
Now that you've seen the different numeric types, write code that calculates the area of a circle whose radius is 2.50 centimeters. Remember that the area of a circle is the radius squared multiplied by PI. One hint: .NET contains a constant for PI, <xref:System.Math.PI?displayProperty=nameWithType> that you can use for that value. <xref:System.Math.PI?displayProperty=nameWithType>, like all constants declared in the `System.Math` namespace, is a `double` value. For that reason, you should use `double` instead of `decimal` values for this challenge.
124217

125218
You should get an answer between 19 and 20.
126219

@@ -135,7 +228,7 @@ Once you try it, open the details pane to see how you did:
135228

136229
Try some other formulas if you'd like.
137230

138-
You completed the "Numbers in C#" interactive tutorial. You can select the **Tuples and types** link to start the next interactive tutorial, or you can visit the [.NET site](https://dotnet.microsoft.com/learn/dotnet/hello-world-tutorial/intro) to download the .NET SDK, create a project on your machine, and keep coding. The "Next steps" section brings you back to these tutorials.
231+
You completed the "Numbers in C#" tutorial. You can select the **Tuples and types** link to start the next tutorial, or you can visit the [.NET site](https://dotnet.microsoft.com/learn/dotnet/hello-world-tutorial/intro) to download the .NET SDK, create a project on your machine, and keep coding. The "Next steps" section brings you back to these tutorials.
139232

140233
You can learn more about numbers in C# in the following articles:
141234

docs/csharp/tour-of-csharp/tutorials/snippets/NumbersInCsharp/NumbersInCsharp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net9.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>

docs/csharp/tour-of-csharp/tutorials/snippets/NumbersInCsharp/Program.cs

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
11

2-
PageOne();
3-
PageTwo();
4-
MorePageTwo();
5-
PageThree();
6-
PageFour();
7-
PageFive();
8-
9-
// Bonus calculation:
10-
// <ChangeDoubleValues>
11-
double a = 19;
12-
double b = 23;
13-
double c = 8;
14-
double d = (a + b) / c;
15-
Console.WriteLine(d);
16-
// </ChangeDoubleValues>
17-
18-
19-
void PageOne()
2+
WorkWithIntegers();
3+
OrderPrecedence();
4+
QuotientAndRemainder();
5+
FloatingPoint();
6+
Decimal();
7+
Challenge();
8+
9+
void WorkWithIntegers()
2010
{
2111
// <Addition>
2212
int a = 18;
2313
int b = 6;
2414
int c = a + b;
2515
Console.WriteLine(c);
2616
// </Addition>
17+
18+
//<OtherOperations>
19+
// subtraction
20+
c = a - b;
21+
Console.WriteLine(c);
22+
23+
// multiplication
24+
c = a * b;
25+
Console.WriteLine(c);
26+
27+
// division
28+
c = a / b;
29+
Console.WriteLine(c);
30+
// </OtherOperations>
2731
}
2832

29-
void PageTwo()
33+
void OrderPrecedence()
3034
{
3135
// <Precedence>
3236
int a = 5;
@@ -45,20 +49,17 @@ void PageTwo()
4549
d = (a + b) - 6 * c + (12 * 4) / 3 + 12;
4650
Console.WriteLine(d);
4751
// </CompoundExpression>
48-
}
4952

50-
void MorePageTwo()
51-
{
5253
// <Truncation>
53-
int a = 7;
54-
int b = 4;
55-
int c = 3;
56-
int d = (a + b) / c;
57-
Console.WriteLine(d);
54+
int e = 7;
55+
int f = 4;
56+
int g = 3;
57+
int h = (e + f) / g;
58+
Console.WriteLine(h);
5859
// </Truncation>
5960
}
6061

61-
void PageThree()
62+
void QuotientAndRemainder()
6263
{
6364
// <QuotientAndRemainder>
6465
int a = 7;
@@ -80,10 +81,9 @@ void PageThree()
8081
int what = max + 3;
8182
Console.WriteLine($"An example of overflow: {what}");
8283
// </Overflow>
83-
8484
}
8585

86-
void PageFour()
86+
void FloatingPoint()
8787
{
8888
// <FloatingPoint>
8989
double a = 5;
@@ -105,7 +105,7 @@ void PageFour()
105105
// </RoundingError>
106106
}
107107

108-
void PageFive()
108+
void Decimal()
109109
{
110110
// <Decimal>
111111
decimal min = decimal.MinValue;
@@ -129,3 +129,15 @@ void PageFive()
129129
Console.WriteLine(area);
130130
// </Challenge>
131131
}
132+
133+
void Challenge()
134+
{
135+
// <ChangeDoubleValues>
136+
double a = 19;
137+
double b = 23;
138+
double c = 8;
139+
double d = (a + b) / c;
140+
Console.WriteLine(d);
141+
// </ChangeDoubleValues>
142+
}
143+

0 commit comments

Comments
 (0)