← C# from Scratch Lesson 10 of 39 · 10 XP

Math

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

The Math class is ready-made maths:

Math.Max(5, 10)     // 10
Math.Min(5, 10)     // 5
Math.Abs(-4.7)      // 4.7
Math.Sqrt(64)       // 8
Math.Pow(2, 10)     // 1024 — returns a double
Math.Round(2.567, 2)// 2.57
Math.Floor(2.9)     // 2 — down
Math.Ceiling(2.1)   // 3 — up
Math.PI             // 3.14159…

Careful: Math.Round(2.5) gives 2, while Math.Round(3.5) gives 4: by default it rounds "to even" (banker's rounding). The familiar "away from zero" is Math.Round(2.5, MidpointRounding.AwayFromZero) → 3.

To print exactly two decimal places: x.ToString("F2") or $"{x:F2}".

Task. Read the legs a and b of a right triangle (whole numbers, separated by a space). Print the hypotenuse with two decimal places, and on the second line, the larger of the two legs.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.