You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
5
5
---
6
6
# How to use integer and floating point numbers in C\#
7
7
8
8
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.
9
9
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
13
11
14
12
## Explore integer math
15
13
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:
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:
21
28
22
29
-`-` for subtraction
23
30
-`*` for multiplication
24
31
-`/` for division
25
32
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`:
Run this code by typing `dotnet run` in your command window.
31
38
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.
33
40
34
41
> [!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
+
voidWorkWithIntegers()
50
+
{
51
+
inta=18;
52
+
intb=6;
53
+
intc=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
+
```
37
70
38
71
## Explore order of operations
39
72
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`:
The output demonstrates that the multiplication is performed before the addition.
45
86
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:
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:
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
+
voidWorkWithIntegers()
110
+
{
111
+
inta=18;
112
+
intb=6;
113
+
intc=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
+
voidOrderPrecedence()
131
+
{
132
+
inta=5;
133
+
intb=4;
134
+
intc=2;
135
+
intd=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
+
inte=7;
145
+
intf=4;
146
+
intg=3;
147
+
inth= (e+f) /g;
148
+
Console.WriteLine(h);
149
+
}
150
+
```
59
151
60
152
## Explore integer precision and limits
61
153
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()`:
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:
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:
@@ -77,35 +169,35 @@ There are other numeric types with different limits and precision that you would
77
169
78
170
## Work with the double type
79
171
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:
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:
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:
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.
99
189
100
190
***Challenge***
101
191
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`.
103
193
104
194
## Work with decimal types
105
195
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:
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:
109
201
110
202
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:
111
203
@@ -117,10 +209,11 @@ The `M` suffix on the numbers is how you indicate that a constant should use the
117
209
118
210
> [!NOTE]
119
211
> 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.
120
213
121
214
***Challenge***
122
215
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.
124
217
125
218
You should get an answer between 19 and 20.
126
219
@@ -135,7 +228,7 @@ Once you try it, open the details pane to see how you did:
135
228
136
229
Try some other formulas if you'd like.
137
230
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.
139
232
140
233
You can learn more about numbers in C# in the following articles:
0 commit comments