Tuesday, September 22, 2015

Write a C Program to accept three sides of triangle from console and to test and print the type of triangle-Equilateral, Isosceles, Right angle, none of these.

Write a C Program to accept three sides of triangle from console and to test and print the type of triangle-Equilateral, Isosceles, Right angle, none of these.

#include<stdio.h>
#include<math.h>

int main() {
  int a, b, c;
  float s, area;

  printf("Enter the values of the sides of the triangle: \n");
  scanf("%d %d %d", &a, &b, &c);                                    //Input three sides of triangle
  if ((a + b > c && a + c > b && b + c > a) && (a > 0 && b > 0 && c > 0))   //test sides using IF ELSE. Here if else uses logical AND operator(&&) which checks for the conditions and return true if all the conditions are true, else it will return falls.
{
    s = (a + b + c) / 2.0;
    area = sqrt((s * (s - a) * (s - b) * (s - c)));    //"sqrt" function determines square root o the given term
    if (a == b && b == c) {
      printf("Equilateral Triangle. \n");
      printf("Area of Equilateral Triangle is: %f", area);
    }
    else if (a == b || b == c || a == c) {
      printf("Isosceles Triangle. \n");
      printf("Area of an Isosceles Triangle: %f", area);
    }
    else {
      printf("Scalene Triangle. \n");
      printf("Area of Scalene Triangle: %f", area);
    }
  }
  else {
    printf("Triangle formation not possible");
  }

  return 0;
}


//************Description****************
// Above program accepts three sides of triangle and determines its type by using IF-//ELSE statement.
//For more description leave your comment below

No comments:

Post a Comment