0Pricing
C# Academy · Lesson

The Math Class

Round, sqrt, pow, and friends.

The Math Class is a free C# Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Meet the Math Class

The System.Math class provides ready-made mathematical functions and constants.

Its members are static, so you call them on the class itself, like Math.Sqrt(16), without creating an object.

double root = Math.Sqrt(16);
Console.WriteLine(root);

Powers and Roots

Math.Pow(x, y) raises x to the power y, and Math.Sqrt(x) returns the square root.

Both return a double, even when the inputs are whole numbers.

Console.WriteLine(Math.Pow(2, 10));
Console.WriteLine(Math.Sqrt(144));

Absolute Value

Math.Abs(x) returns the absolute value, removing any negative sign.

This is useful when you care about magnitude or distance rather than direction.

Console.WriteLine(Math.Abs(-7));
Console.WriteLine(Math.Abs(3.5));

Min and Max

Math.Min and Math.Max return the smaller or larger of two values.

They work with integers, doubles, and other numeric types.

Console.WriteLine(Math.Max(8, 12));
Console.WriteLine(Math.Min(8, 12));

Rounding Numbers

Math.Round(x) rounds to the nearest whole number. You can pass a second argument for the number of decimal places.

By default it uses banker's rounding, where exactly halfway values round to the nearest even number.

Console.WriteLine(Math.Round(3.456, 2));
Console.WriteLine(Math.Round(2.5));

Floor and Ceiling

Math.Floor(x) rounds down to the nearest integer, and Math.Ceiling(x) rounds up.

They always go toward negative or positive infinity, regardless of the fraction.

Console.WriteLine(Math.Floor(3.9));
Console.WriteLine(Math.Ceiling(3.1));

Truncation

Math.Truncate(x) simply drops the fractional part without rounding.

Unlike Math.Floor, it moves toward zero, so Math.Truncate(-3.9) gives -3.

Console.WriteLine(Math.Truncate(3.9));
Console.WriteLine(Math.Truncate(-3.9));

Math Constants

The class exposes two useful constants: Math.PI (approximately 3.14159) and Math.E (approximately 2.71828).

Use them in geometry and exponential formulas instead of typing the digits yourself.

double radius = 5;
double area = Math.PI * radius * radius;
Console.WriteLine(area);

Sign and Comparisons

Math.Sign(x) returns -1, 0, or 1 depending on whether a number is negative, zero, or positive.

It is a quick way to learn the direction of a value.

Console.WriteLine(Math.Sign(-42));
Console.WriteLine(Math.Sign(0));
Console.WriteLine(Math.Sign(99));

Combining Math Methods

You can nest and combine Math methods to build real formulas. Here we compute the distance between two points using the Pythagorean theorem.

double dx = 3, dy = 4;
double distance = Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));
Console.WriteLine(distance);

When to Use Math

Reach for the Math class whenever you need:

  • Powers, roots, or logarithms
  • Rounding, flooring, or ceiling
  • Absolute value, min, or max
  • Constants like PI

It saves you from reinventing common formulas.

Quick Check

Check your understanding of the Math class.

Recap

You explored the System.Math class: Sqrt, Pow, Abs, Min, Max, and the rounding family Round, Floor, Ceiling, and Truncate.

You also met the constants Math.PI and Math.E and Math.Sign. These static helpers cover most everyday math you'll need.

Frequently asked questions

Is the “The Math Class” lesson free?

Yes — the full text of “The Math Class” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “The Math Class”?

Round, sqrt, pow, and friends. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The Math Class” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Numeric Types
  2. Arithmetic and Precedence
  3. The Math Class
  4. Converting Between Numbers
← Back to C# Academy