Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 38 · The Laws of Sines and Cosines

The Law of Cosines

Learn the law of cosines as a generalized Pythagorean theorem, solve triangles from SAS and SSS, and find the largest angle safely with code.

The law of sines needs a matched side and angle. If you know two sides and the angle between them (SAS), or all three sides (SSS), no side-angle pair exists yet, so you need another tool. The law of cosines is that tool. It is the Pythagorean theorem with a correction term for when the angle is not 90°.

The law

Text
c² = a² + b² − 2ab cos C
a² = b² + c² − 2bc cos A
b² = a² + c² − 2ac cos B

Each form has the side being found on the left, and its opposite angle in the cosine. If C = 90°, then cos C = 0 and the law becomes c² = a² + b²: the Pythagorean theorem. If C is acute, cos C > 0 and c² < a² + b²; if C is obtuse, cos C < 0 and c² > a² + b². That is a quick way to classify a triangle from its sides.

Why it works

Place vertex C at the origin with side a along the positive x-axis. Then B = (a, 0) and A = (b cos C, b sin C). The distance AB = c is

Text
c² = (b cos C − a)² + (b sin C)²
   = b² cos²C − 2ab cos C + a² + b² sin²C
   = a² + b²(cos²C + sin²C) − 2ab cos C
   = a² + b² − 2ab cos C

SAS: find the third side

Example. Two sides of 7 cm and 8 cm enclose an angle of 60°. Find the third side.

Text
c² = 7² + 8² − 2(7)(8) cos 60° = 49 + 64 − 112 (0.5) = 57
c = √57 ≈ 7.55 cm

Once you have all three sides, use the law of sines (careful with the ambiguous case!) or the law of cosines again to find the angles. Tip: find the angle opposite the smaller of the two remaining sides with the law of sines, because that angle must be acute, so arcsin returns the right answer.

SSS: find the angles

Rearrange the law to solve for the cosine:

Text
cos C = (a² + b² − c²) / (2ab)

Example. A triangle has sides 5, 6 and 7. Find all angles.

  • Largest angle is opposite 7: cos C = (25 + 36 − 49)/(2·5·6) = 12/60 = 0.2, so C ≈ 78.46°.
  • cos B = (25 + 49 − 36)/(2·5·7) = 38/70 ≈ 0.5429, so B ≈ 57.12°.
  • A = 180° − 78.46° − 57.12° = 44.42°.

Why use arccos here? Because cosine distinguishes acute from obtuse angles (its sign), while arcsin cannot: it would return the acute twin of an obtuse angle. Finding the largest angle first with arccos tells you immediately whether the triangle is obtuse.

Python Law of cosines calculator: SAS and SSS

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Triangle inequality

Three lengths form a triangle only if the sum of any two exceeds the third. For 2, 3, 9, 2 + 3 = 5 < 9, so no triangle exists. In the formula this appears as a cosine whose value falls outside [−1, 1].

Classifying a triangle by its sides

Let c be the longest side. Compare c² with a² + b²:

Condition Triangle
c² < a² + b² acute
c² = a² + b² right
c² > a² + b² obtuse

So 4, 5, 6 is acute (36 < 41), 5, 12, 13 is right (169 = 169), and 4, 5, 8 is obtuse (64 > 41).

Python Classify triangles from their sides

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Choosing the right law

You know Use
AAS or ASA law of sines
SSA law of sines (check for two solutions)
SAS law of cosines for the third side, then either law
SSS law of cosines for the largest angle, then either law

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Sides a = 9 and b = 12 enclose an angle C = 90°. What does the law of cosines give for c?

    cos 90° = 0, so c² = 81 + 144 = 225 and c = 15, the Pythagorean theorem.
  2. 2. A triangle has sides 4, 5 and 8. Is it acute, right or obtuse?

    The longest side squared is 64, which is greater than 16 + 25 = 41, so the largest angle is obtuse.
  3. 3. Why is arccos preferred over arcsin when finding the largest angle from three sides?

    arccos returns an angle from 0° to 180°, so an obtuse angle is found correctly; arcsin returns an acute angle even when the true angle is obtuse.
  4. 4. In c² = a² + b² − 2ab cos C, if C is obtuse the term −2ab cos C is:

    cos C is negative for obtuse C, so −2ab cos C is positive and c² is larger than a² + b².

Summary

  • c² = a² + b² − 2ab cos C; the angle sits opposite the side on the left.
  • With C = 90° it becomes the Pythagorean theorem.
  • SAS: find the third side directly. SSS: cos C = (a² + b² − c²) / (2ab).
  • Use arccos for the largest angle; it tells you if the triangle is obtuse.
  • Check the triangle inequality: the two shorter sides must add up to more than the longest.

Next: Area of a Triangle and Heron's Formula