Write a float function triangle that computes the area of triangle using its two formal parameters h and w, where h is height and w is the length of bases of triangle?
#include <iostream>
float triangle(float h, float w)
{
return h * w / 2;
}
int main()
{
std::cout << "Please enter the height and the length of bases of triangle: ";
float h, w;
std::cin >> h >> w;
if(!std::cin || h < 0 || w < 0)
{
std::cout << "Bad input\n";
return 1;
}
std::cout << "The area of triangle is " << triangle(h, w) << "\n";
return 0;
}
Comments
Leave a comment