Manipulate integral and floating point numbers
This tutorial teaches you about the numeric types in C# interactively, using your browser. You'll write C# and see the results of compiling and running your code. It contains a series of lessons that explore numbers and math operations in C#. These lessons teach you the fundamentals of the C# language.
Tip
To paste a code snippet inside the focus mode you should use your keyboard shortcut (Ctrl + v, or cmd + v).
Explore integer math
Run the following code in the interactive window. Select the Enter focus mode button. Then, type the following code block in the interactive window and select Run:
int a = 18;
int b = 6;
int c = a + b;
Console.WriteLine(c);
If you're working on your environment, you should follow the instructions for the local version instead.
You've seen one of the 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:
-
for subtraction*
for multiplication/
for division
Tip
Throughout this interactive tutorial, you can explore on your own by modifying the code you've written in the interactive window. This tutorial provides examples that you can try at each step.
Start by exploring those different operations. Modify the third line to try each of these operations. After each edit, select the Run button.
Subtraction:
int c = a - b;
Multiplication:
int c = a * b;
Division:
int c = a / b;
You can also experiment by writing multiple mathematics operations in the same line, if you'd like.
Tip
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 the interactive window to see what to fix. That exercise will help you learn the structure of C# code.
Explore order of operations
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:
int a = 5;
int b = 4;
int c = 2;
int d = a + b * c;
Console.WriteLine(d);
The output demonstrates that the multiplication is performed before the addition.
You can force a different order of operation by adding parentheses around the operation or operations you want performed first:
int a = 5;
int b = 4;
int c = 2;
int d = (a + b) * c;
Console.WriteLine(d);
Explore more by combining many different operations. Replace the fourth line above with something like this:
int d = (a + b) - 6 * c + (12 * 4) / 3 + 12;
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.
If you haven't seen this behavior, try the following code:
int a = 7;
int b = 4;
int c = 3;
int d = (a + b) / c;
Console.WriteLine(d);
Select Run again to see the results.
Explore integer precision and limits
That last sample showed you that integer division truncates the result.
You can get the remainder by using the remainder operator, the %
character:
int a = 7;
int b = 4;
int c = 3;
int d = (a + b) / c;
int e = (a + b) % c;
Console.WriteLine($"quotient: {d}");
Console.WriteLine($"remainder: {e}");
The C# integer type differs from mathematical integers in one other
way: the int
type has minimum and maximum limits. Run this code
in the interactive window to see those limits:
int max = int.MaxValue;
int min = int.MinValue;
Console.WriteLine($"The range of integers is {min} to {max}");
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. Add these two lines to the interactive window to see an example:
int what = max + 3;
Console.WriteLine($"An example of overflow: {what}");
Notice that the answer is very close to the minimum (negative) integer. It's
the same as min + 2
.
The addition operation overflowed the allowed values for integers.
The answer is a very large negative number because an overflow "wraps around"
from the largest possible integer value to the smallest.
There are other numeric types with different limits and precision that you
would use when the int
type doesn't meet your needs. Let's explore those types of numbers next.
Work with the double type
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
numbers 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 is more common to use double precision than single precision numbers. Single precision numbers are declared using the float
keyword.
Let's explore. Try the following code in the interactive window and see the result:
double a = 5;
double b = 4;
double c = 2;
double d = (a + b) / c;
Console.WriteLine(d);
Notice that the answer includes the decimal portion of the quotient. Try a slightly more complicated expression with doubles:
double a = 19;
double b = 23;
double c = 8;
double d = (a + b) / c;
Console.WriteLine(d);
The range of a double value is much greater than integer values. Try the following code in the interactive window:
double max = double.MaxValue;
double min = double.MinValue;
Console.WriteLine($"The range of double is {min} to {max}");
These values are printed out 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:
double third = 1.0 / 3.0;
Console.WriteLine(third);
You know that 0.3
is 3/10
and not exactly the same as 1/3
. Similarly, 0.33
is 33/100
. That's closer to 1/3
, but still not exact.
Challenge
Try other calculations with large numbers, small numbers, multiplication,
and division using the double
type. Try more complicated calculations.
Work with decimal types
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:
decimal min = decimal.MinValue;
decimal max = decimal.MaxValue;
Console.WriteLine($"The range of the decimal type is {min} to {max}");
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:
double a = 1.0;
double b = 3.0;
Console.WriteLine(a / b);
decimal c = 1.0M;
decimal d = 3.0M;
Console.WriteLine(c / d);
Notice that the math using the decimal type has more digits to the right of the decimal point.
The M
suffix on the numbers is how you indicate that a constant should use the decimal
type. Otherwise, the compiler assumes the double
type.
Note
The letter M
was chosen as the most visually distinct letter between the double
and decimal
keywords.
Challenge
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, Math.PI that you can use for that value. Math.PI, 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.
You should get an answer between 19 and 20.
Complete challenge
Did you come up with something like this?
double radius = 2.50;
double area = Math.PI * radius * radius;
Console.WriteLine(area);
Try some other formulas if you'd like.
Congratulations!
You've completed the "Numbers in C#" interactive tutorial. You can select the Branches and Loops link below to start the next interactive tutorial, or you can visit the .NET site to download the .NET SDK, create a project on your machine, and keep coding. The "Next steps" section brings you back to these tutorials.
You can learn more about numbers in C# in the following articles:
Have an issue with this section? If so, please give us some feedback so we can improve this section.