R Program to Calculate the Area of a Triangle
In this article, we will discuss how to calculate the Area of a Triangle with its working example in the R programming language. Calculating the area of a triangle is a fundamental geometric task. The area of a triangle can be computed using the base and height of the triangle or by using the lengths of its sides and the semi-perimeter. In this R program, we will explore how to calculate the area of a triangle using the base and height methods.
Syntax:
# Calculate the area of the triangle
area <- 0.5 * base * height
# Print the result
cat("The area of the triangle is:", area)
Example 1:
R
# Values for base and height base <- 10 height <- 5 # Calculate the area of the triangle area <- 0.5 * base * height # Print the result cat ( "The area of the triangle is:" , area) |
Output:
The area of the triangle is: 25
- Assigns the value 10 to the variable base, representing the length of the base of the triangle.
- Assigns the value 5 to the variable height, representing the height of the triangle.
- Calculates the area of the triangle using the formula 0.5 * base * height and stores the result in the variable area.
- Uses the cat() function to print the message.
Example 2:
R
# Values for base and height base <- 10 height <- 10 # Calculate the area of the triangle area <- 0.5 * base * height # Print the result cat ( "The area of the triangle is:" , area) |
Output:
The area of the triangle is: 50
- Assigns the value 10 to the variable base, representing the length of the base of the triangle.
- Assigns the value 10 to the variable height, representing the height of the triangle.
- Calculates the area of the triangle using the formula 0.5 * base * height and stores the result in the variable area.
- Uses the cat() function to print the message.
Example 3: R Program to Calculate the Area of a Triangle using Side Lengths
R
# Function to calculate the area of a triangle using side lengths calculateTriangleArea <- function (a, b, c) { s <- (a + b + c) / 2 area <- sqrt (s * (s - a) * (s - b) * (s - c)) return (area) } # Input: Lengths of the three sides of the triangle a <- 5 b <- 5 c <- 5 # Calculate the area using the function triangleArea <- calculateTriangleArea (a, b, c) # Display the result cat ( "\nThe area of the triangle with side lengths" , a, b, c, "is:" , triangleArea, "\n" ) |
Output:
The area of the triangle with side lengths 5 5 5 is: 10.82532
- Define a function named calculateTriangleArea that takes three parameters a, b, and c.
- Inside the function, calculate the semi-perimeter s of the triangle by adding a, b, and c and then dividing by 2.
- Calculate the area of the triangle using the formula for the area of a triangle given its side lengths: area = sqrt(s * (s – a) * (s – b) * (s – c)).
- Return the calculated area from the function.
- Assign the value 5 to variables a, b, and c, representing the side lengths of the triangle.
- Call the calculateTriangleArea function with the side lengths a, b, and c as arguments. The calculated area is returned and stored in the variable triangleArea.
- Display a message using cat, indicating the area of the triangle with side lengths a, b, and c, and showing the calculated triangleArea.