Triangle area code generator
The triangle area code generator writes a ready-to-run program for your numbers in 7 languages: Python, Java, C, C++, JavaScript, Excel, and Google Sheets, and shows the output the program prints.
Triangle area formula in Python
The triangle area formula in Python is area = 0.5 * base * height, and the math module adds math.sqrt() for Heron's formula and math.sin() for the angle formula.
The program below defines 4 functions, 1 for each method, and prints a tested result for each. Python 3 division with / always returns a float, so 1/2 equals 0.5.
import math
def area_base_height(base, height):
return 0.5 * base * height
def area_heron(a, b, c):
if a + b <= c or a + c <= b or b + c <= a:
raise ValueError("These sides do not form a triangle")
s = (a + b + c) / 2
return math.sqrt(s * (s - a) * (s - b) * (s - c))
def area_sas(a, b, angle_deg):
return 0.5 * a * b * math.sin(math.radians(angle_deg))
def area_coords(x1, y1, x2, y2, x3, y3):
return abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2
print(area_base_height(10, 6)) # 30.0
print(area_heron(5, 6, 7)) # 14.696938456699069
print(round(area_sas(8, 6, 30), 2)) # 12.0
print(area_coords(1, 1, 7, 2, 3, 6)) # 14.0Python program that reads the base and height
base = float(input("Base: "))
height = float(input("Height: "))
print(f"Area = {0.5 * base * height:.2f}")Input 10 and 6 prints Area = 30.00. NumPy users get the same coordinate result with 0.5 * abs(np.cross(B - A, C - A)) on 2D arrays.
Triangle area formula in Java
The triangle area formula in Java is double area = 0.5 * base * height;, with Math.sqrt() and Math.sin(Math.toRadians(angle)) for the other 2 methods.
public class TriangleArea {
static double baseHeight(double base, double height) {
return 0.5 * base * height;
}
static double heron(double a, double b, double c) {
double s = (a + b + c) / 2;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}
static double sas(double a, double b, double angleDeg) {
return 0.5 * a * b * Math.sin(Math.toRadians(angleDeg));
}
public static void main(String[] args) {
System.out.println(baseHeight(10, 6)); // 30.0
System.out.println(heron(5, 6, 7)); // 14.696938456699069
System.out.printf("%.2f%n", sas(8, 6, 30)); // 12.00
}
}Save the file as TriangleArea.java, then run javac TriangleArea.java and java TriangleArea. Declare every length as double: int values give wrong results with 1/2.
Triangle area formula C program
A triangle area formula C program reads the base and height with scanf("%lf %lf", ...) and prints 0.5 * base * height with printf("%.2f").
#include <stdio.h>
#include <math.h>
double heron(double a, double b, double c) {
double s = (a + b + c) / 2.0;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
int main(void) {
double base, height;
printf("Enter base and height: ");
if (scanf("%lf %lf", &base, &height) != 2) return 1;
printf("Area = %.2f\n", 0.5 * base * height); /* 10 6 -> Area = 30.00 */
printf("Heron 5 6 7 = %.2f\n", heron(5, 6, 7)); /* 14.70 */
return 0;
}Compile with gcc triangle.c -o triangle -lm. The -lm flag links the maths library that holds sqrt() and sin(). Input 10 6 prints Area = 30.00 and Heron 5 6 7 = 14.70.
Triangle area formula in C++ and JavaScript
The triangle area formula in C++ and JavaScript is 0.5 * base * height, with std::sqrt and std::sin in C++ and Math.sqrt and Math.sin in JavaScript.
#include <cmath>
#include <iostream>
double area(double base, double height) { return 0.5 * base * height; }
int main() {
std::cout << area(10, 6) << "\n"; // 30
}const areaBaseHeight = (b, h) => 0.5 * b * h;
const areaHeron = (a, b, c) => {
const s = (a + b + c) / 2;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
};
const areaSas = (a, b, deg) => 0.5 * a * b * Math.sin(deg * Math.PI / 180);
console.log(areaBaseHeight(10, 6)); // 30
console.log(areaHeron(5, 6, 7).toFixed(2)); // 14.70Triangle area formula in Excel and Google Sheets
The triangle area formula in Excel is =0.5*A2*B2, with the base in cell A2 and the height in cell B2. The same formulas work in Google Sheets.
| Method | Cells | Formula |
|---|---|---|
| Base and height | A2 = base, B2 = height | =0.5*A2*B2 |
| 3 sides (Excel 365, Sheets) | A2, B2, C2 = sides | =LET(s,(A2+B2+C2)/2,SQRT(s*(s-A2)*(s-B2)*(s-C2))) |
| 3 sides (older Excel) | D2 = (A2+B2+C2)/2 | =SQRT(D2*(D2-A2)*(D2-B2)*(D2-C2)) |
| 2 sides + angle | A2, B2 = sides, C2 = angle in ° | =0.5*A2*B2*SIN(RADIANS(C2)) |
| Coordinates | A2:F2 = x₁, y₁, x₂, y₂, x₃, y₃ | =ABS(A2*(D2-F2)+C2*(F2-B2)+E2*(B2-D2))/2 |
| Check sides | A2, B2, C2 = sides | =IF(MAX(A2:C2)<SUM(A2:C2)-MAX(A2:C2),"valid","not a triangle") |
Fill the formula down a column to find the area of many triangles at once. MATLAB users get the coordinate area with polyarea([x1 x2 x3], [y1 y2 y3]).
4 common triangle area code bugs
There are 4 common triangle area code bugs: integer division, degrees passed to sin(), invalid sides under the square root, and a missing absolute value in the coordinate formula.
| Bug | Wrong output | Fix |
|---|---|---|
Integer division: 1/2*b*h in C, C++, or Java | 0 | Write 0.5*b*h or b*h/2.0 |
Degrees in sin(): sin(30) | −23.71 for sides 8 and 6 | Convert: sin(30*π/180) gives 12 |
| Invalid sides in Heron's formula: 3, 4, 8 | NaN | Check each side < the sum of the other 2 |
| No abs() in the coordinate formula | −14 for clockwise points | Wrap the sum in abs() |
Test cases for a triangle area program
| Input | Expected area |
|---|---|
| Base 10, height 6 | 30 |
| Sides 3, 4, 5 | 6 |
| Sides 13, 14, 15 | 84 |
| Sides 8 and 6, angle 30° | 12 |
| Points (1, 1), (7, 2), (3, 6) | 14 |
| Sides 3, 4, 8 | Error: not a triangle |
Compare your program's output with the triangle area formula calculator, or practise by hand with the triangle area formula examples and worksheet.
Triangle area formula in code FAQs
How do you write the triangle area formula in Python?
You write the triangle area formula in Python as area = 0.5 * base * height. Heron's formula uses math.sqrt(s * (s - a) * (s - b) * (s - c)) with s = (a + b + c) / 2.
How do you write a C program for the area of a triangle?
You write a C program for the area of a triangle with double variables, scanf("%lf %lf", &base, &height), and 0.5 * base * height. Heron's formula needs #include <math.h> and the -lm flag when you compile with gcc.
Why does my triangle area program print 0?
Your triangle area program prints 0 since 1/2 is integer division in C, C++, and Java, and equals 0. Write 0.5 or 1.0/2 instead, so 1/2 × 10 × 6 becomes 30, not 0.
How do you calculate triangle area in Excel?
You calculate triangle area in Excel with =0.5*A2*B2, where A2 holds the base and B2 holds the height. For 3 sides, use =LET(s,(A2+B2+C2)/2,SQRT(s*(s-A2)*(s-B2)*(s-C2))) in Excel 365.
What is the triangle area formula called in Excel?
Excel has no built-in triangle area function, so you type the formula yourself: =0.5*A2*B2 for base and height. Name the result cell with Formulas › Define Name, such as TriangleArea, to reuse it in other formulas.
Why is my sine area formula wrong in code?
Your sine area formula is wrong since sin() in Python, Java, C, JavaScript, and Excel takes radians, not degrees. Convert first: math.radians(30) in Python, Math.toRadians(30) in Java, and RADIANS(30) in Excel.
Why does Heron's formula return NaN in my program?
Heron's formula returns NaN when the 3 sides fail the triangle inequality, so the value under the square root is negative. Sides 3, 4, and 8 give s = 7.5 and s(s−a)(s−b)(s−c) = −59.06. Check that each side is shorter than the sum of the other 2.