CS-101 Lab 1 Write a program that computes the area of a triangle using Heron\'s
ID: 641171 • Letter: C
Question
CS-101 Lab 1 Write a program that computes the area of a triangle using Heron's formula Prompt the user for the lengths of the three sides, say a, b, and c Heron's formula is: Area ,s(s-a)(s-b)(s-c), where s-a++ c , the semi-perimeter. The program should avoid trying to compute the square root of a negative number. Heron's formula i Are te-oX-bx-., hte s-bXs-), where bts the semi-perimeter. 2 (Your program has to check negative square roots) The output should look like the one of following: The triangle with sides: #1, #2, #3 is not a triangle (Square root of a negative number) The triangle with sides: #1, #2, #3 has semi-perimeter: #p and area: #a Test your program with these values for (a, b, c): (5, 7, 9), (6.2, 3.7, 4.5), (15, 11, 8.1), (8, 15, 17), (6, 15, 8) and two more triples of your choice. Coding Process (Suggested): 1. Create a shell program that compiles 2. Create all variables that your program will require 3. Code the user input part for all the variables 4. Check if the area is valid (Not the square root of a negative number) 5. Compute the Area using the input values 6. Output the area to the screen and allow the user to see it before exiting. 1. Submit both the source code and the outout AS ONE FUExplanation / Answer
#include<iostream>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
int main(){
cout << "enter the sides os a triangle:" << endl;
float a,b,c,area;
cin >> a;
cin >> b;
cin >> c;
float s = (a+b+c)/2;
int i=0;
if ((s-a)<=0)
i++;
if((s-b)<=0)
i++;
if((s-c)<=0)
i++;
if (i%2==1)
{
cout << "the given sides donot form a triangle" << endl;
exit(1);
}
else
{
area = sqrt(s*(s-a)*(s-b)*(s-c));
cout << "the area of triangle :" << area <<endl;
}
getch();
return 0;
}