Program to find area of triangle
- Write a program to find area of triangle in C
- Write a program to find area of triangle in C++
- Write a program to find area of triangle in Python
- Write a program to find area of triangle in PHP
- Write a program to find area of triangle in Java
- Write a program to find area of triangle in Java Script
- Write a program to find area of triangle in C#
Explanation:
The area of a triangle can be calculated using various methods depending on the information available:
1. Base and Height
Area = 0.5 × Base × Height
2. Heron’s Formula (if the lengths of all three sides are known)
s = (a + b + c)/2
Area = sqrt(s x (s – a) x (s – b) x (s – c))
Where a, b, and c are the sides of the triangle, and is the semi-perimeter.
3. Using Trigonometry (if two sides and the included angle are known)
Area = 0.5 x a x b x sin(θ)
Where a and b are the lengths of two sides, and θ is the included angle in radians.
Program to find area of triangle
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h> int main() { float base, height, area; printf("Enter the base of the triangle: "); scanf("%f", &base); printf("Enter the height of the triangle: "); scanf("%f", &height); area = 0.5 * base * height; printf("Area of the triangle: %.2f\n", area); return 0; }
#include <iostream> using namespace std; int main() { double base, height, area; cout << "Enter the base of the triangle: "; cin >> base; cout << "Enter the height of the triangle: "; cin >> height; area = 0.5 * base * height; cout << "Area of the triangle: " << area << endl; return 0; }
base = float(input("Enter the base of the triangle: ")) height = float(input("Enter the height of the triangle: ")) area = 0.5 * base * height print(f"Area of the triangle: {area:.2f}")
<?php echo "Enter the base of the triangle: "; $base = (float)readline(); echo "Enter the height of the triangle: "; $height = (float)readline(); $area = 0.5 * $base * $height; echo "Area of the triangle: " . number_format($area, 2) . "\n"; ?>
import java.util.Scanner; public class TriangleArea { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the base of the triangle: "); double base = scanner.nextDouble(); System.out.print("Enter the height of the triangle: "); double height = scanner.nextDouble(); double area = 0.5 * base * height; System.out.printf("Area of the triangle: %.2f\n", area); scanner.close(); } }
const base = parseFloat(prompt("Enter the base of the triangle:")); const height = parseFloat(prompt("Enter the height of the triangle:")); const area = 0.5 * base * height; console.log(`Area of the triangle: ${area.toFixed(2)}`);
using System; class Program { static void Main() { Console.Write("Enter the base of the triangle: "); double baseLength = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter the height of the triangle: "); double height = Convert.ToDouble(Console.ReadLine()); double area = 0.5 * baseLength * height; Console.WriteLine($"Area of the triangle: {area:F2}"); } }