- Here, we declare three variables
base
, height
, and area
as floats. We then prompt the user to enter the base and height of the triangle using printf()
and scanf()
. - We then calculate the area of the triangle using the formula
area = 0.5 * base * height
, and store the result in the area
variable. Finally, we print out the area using printf()
. - Note that we use
%f
as the format specifier for floats in scanf()
and printf()
.
#include <stdio.h>
int main() {
float base, height, area;
// Get the base and height from the user
printf("Enter the base of the triangle: ");
scanf("%f", &base);
printf("Enter the height of the triangle: ");
scanf("%f", &height);
// Calculate the area
area = 0.5 * base * height;
// Print the area
printf("The area of the triangle is %f\n", area);
return 0;
}
OUTPUT
Enter the base of the triangle: 4
Enter the height of the triangle: 6
The area of the triangle is 12.000000