C# & .NET Framework Variables & Data Types
💡
Exercise 7

Numeric Types — Costing the Digital City 15 XP Easy

Ctrl+Enter Run Ctrl+S Save

🧮 The Architect's Calculator

Your digital city is taking shape, but before a single wall goes up, the .NET Architect must crunch the numbers. How many bricks? What's the cost? Can the foundation handle the load? C# gives you a rich toolkit of numeric types — each one tuned for a different job, like choosing the right measuring tape for the task.

📐 The Numeric Type Family

Think of these as different-sized containers on your workshop shelf:

  • byte (0 to 255) — Tiny counter, like tracking paint cans in a room.
  • short (-32,768 to 32,767) — Small numbers, like bolts per beam.
  • int (-2.1B to 2.1B) — The everyday workhorse. Floor counts, brick totals, IDs.
  • long (±9.2 quintillion) — Massive numbers, like grains of sand on a beach. Suffix: L.
  • float (~7 digits precision) — Quick-and-dirty decimals. Suffix: f.
  • double (~15 digits precision) — The default decimal type. Scientific calculations, measurements.
  • decimal (~28 digits precision) — Money and finance. Never use double for currency! Suffix: m.
int bricks = 1500; decimal costPerBrick = 2.50m; // 'm' suffix for decimal long worldPopulation = 8000000000L; // 'L' suffix for long float temperature = 36.6f; // 'f' suffix for float double pi = 3.141592653589793;

➕ Arithmetic Operators

C# supports all the classic math operations. The one that surprises newcomers is integer division — dividing two int values drops the decimal part entirely.

int a = 10, b = 3; Console.WriteLine(a + b); // 13 (addition) Console.WriteLine(a - b); // 7 (subtraction) Console.WriteLine(a * b); // 30 (multiplication) Console.WriteLine(a / b); // 3 (integer division — truncated!) Console.WriteLine(a % b); // 1 (modulus — remainder)

📚 The Math Class

For anything beyond basic arithmetic, the Math class is your engineering calculator:

Console.WriteLine(Math.Pow(2, 10)); // 1024 (2 to the power of 10) Console.WriteLine(Math.Sqrt(144)); // 12 (square root) Console.WriteLine(Math.Round(3.567)); // 4 (round to nearest) Console.WriteLine(Math.Abs(-42)); // 42 (absolute value) Console.WriteLine(Math.Max(10, 20)); // 20 (larger of two) Console.WriteLine(Math.Min(10, 20)); // 10 (smaller of two)

⚠️ Watch Out: Integer Division & Decimal Precision

7 / 2 gives 3, not 3.5! To get a decimal result, at least one operand must be a decimal type: 7.0 / 2 gives 3.5. And never use double for money — 0.1 + 0.2 might not equal 0.3 due to floating-point quirks. Use decimal instead.

📋 Instructions
### Your Mission The Architect is ordering materials for the city's first building. Calculate the costs! 1. Declare an `int` variable `totalBricks` with value `1500`. 2. Declare a `decimal` variable `costPerBrick` with value `2.50m`. 3. Calculate `totalCost` as `totalBricks * costPerBrick`. 4. Use `Math.Sqrt(144)` and store the result in an `int` variable `sqrtResult` (cast it to `int`). Print the results in this exact format: ``` Total bricks: 1500 Cost per brick: $2.50 Total cost: $3750.00 Square root of 144: 12 ``` > **Tip:** Use `cost.ToString("F2")` or string interpolation with `:F2` to format to 2 decimal places.
Multiply totalBricks * costPerBrick for the total (you may need to cast totalBricks to decimal). For formatting, use $"Cost per brick: ${costPerBrick:F2}" to ensure two decimal places. Cast Math.Sqrt(144) to int with (int).
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.