{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Numbers and Integer Math\n", "\n", "Watch the full [C# 101 video](https://www.youtube.com/watch?v=jEE0pWTq54U&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=5) for this module." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Integer Math\n", "\n", "You have a few `integers` defined below. An `integer` is a positive or negative whole number.\n", "> Before you run the code, what should c be?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Addition" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "24\r\n" ] } ], "source": [ "int a = 18;\n", "int b = 6;\n", "int c = a + b;\n", "Console.WriteLine(c);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Subtraction" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12\r\n" ] } ], "source": [ "int c = a - b;\n", "Console.WriteLine(c);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Multiplication" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "108\r\n" ] } ], "source": [ "int c = a * b;\n", "Console.WriteLine(c);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Division" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\r\n" ] } ], "source": [ "int c = a / b;\n", "Console.WriteLine(c);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Order of operations\n", "\n", "C# follows the order of operation when it comes to math. That is, it does multiplication and division first, then addition and subtraction.\n", "> What would the math be if C# didn't follow the order of operation, and instead just did math left to right?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "13\r\n" ] } ], "source": [ "int a = 5;\n", "int b = 4;\n", "int c = 2;\n", "int d = a + b * c;\n", "Console.WriteLine(d);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using parenthesis\n", "\n", "You can also force different orders by putting parentheses around whatever you want done first\n", "> Try it out" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "18\r\n" ] } ], "source": [ "int d = (a + b) * c;\n", "Console.WriteLine(d);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can make math as long and complicated as you want.\n", "> Can you make this line even more complicated?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "25\r\n" ] } ], "source": [ "int d = (a + b) - 6 * c + (12 * 4) / 3 + 12;\n", "Console.WriteLine(d);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Integers: Whole numbers no matter what\n", "\n", "Integer math will always produce integers. What that means is that even when math should result in a decimal or fraction, the answer will be truncated to a whole number.\n", "> Check it out. WHat should the answer truly be?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\r\n" ] } ], "source": [ "int a = 7;\n", "int b = 4;\n", "int c = 3;\n", "int d = (a + b) / c;\n", "Console.WriteLine(d);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Playground\n", "\n", "Play around with what you've learned! Here's some starting ideas:\n", "> Do you have any homework or projects that need math? Try using code in place of a calculator!\n", ">\n", "> How do integers round? Do they always round up? down? to the nearest integer?\n", ">\n", "> How do the Order of Operations work? Play around with parentheses." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Playground\r\n" ] } ], "source": [ "Console.WriteLine(\"Playground\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Continue learning\n", "\n", "There are plenty more resources out there to learn!\n", "> [⏩ Next Module - Numbers and Integer Precision](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/05-Numbers%20and%20Integer%20Precision.ipynb)\n", ">\n", "> [⏪ Last Module - Searching Strings](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/03-Searching%20Strings.ipynb)\n", ">\n", "> [Watch the video](https://www.youtube.com/watch?v=jEE0pWTq54U&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=5)\n", ">\n", "> [Documentation: Numbers in C#](https://docs.microsoft.com/dotnet/csharp/tour-of-csharp/tutorials/numbers-in-csharp?WT.mc_id=Educationalcsharp-c9-scottha)\n", ">\n", "> [Start at the beginning: What is C#?](https://www.youtube.com/watch?v=BM4CHBmAPh4&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Other resources\n", "\n", "Here's some more places to explore:\n", "> [Other 101 Videos](https://dotnet.microsoft.com/learn/videos?WT.mc_id=csharpnotebook-35129-website)\n", ">\n", "> [Microsoft Learn](https://docs.microsoft.com/learn/dotnet/?WT.mc_id=csharpnotebook-35129-website)\n", ">\n", "> [C# Documentation](https://docs.microsoft.com/dotnet/csharp/?WT.mc_id=csharpnotebook-35129-website)" ] } ], "metadata": { "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "language_info": { "file_extension": ".cs", "mimetype": "text/x-csharp", "name": "C#", "pygments_lexer": "csharp", "version": "8.0" } }, "nbformat": 4, "nbformat_minor": 4 }