Program: Calculating the Area of a Triangle
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
// Check if both base and height are provided
if (argc == 3) {
// Convert command line arguments to floating-point numbers
float base = atof(argv[1]);
float height = atof(argv[2]);
// Check if the provided values are valid
if (base > 0 && height > 0) {
// Calculate the area of the triangle
float area = 0.5 * base * height;
// Display the result
printf("Area of the triangle with base %.2f and height %.2f is: %.2f\n", base, height, area);
} else {
printf("Both base and height must be positive numbers.\n");
}
} else {
printf("Usage: ./triangle_area <base> <height>\n");
}
return 0;
}