Tuesday 11 September 2012

If the three sides of a triangle are entered through the keyboard, write a program to check whether the triangle is isosceles, equilateral, scalene or right angled triangle.


main()
{
 int a,b,c;
 printf("Enter the sides of a triangle :- ");
 scanf("%d%d%d",&a,&b,&c);
 if((a>b && a>c) && a<(b+c) || (b>a && b>c) && b<(a+c) || (c>a && c>b) && c<(a+b) || (a==b && b==c))
 {
  if(a==b && b==c)
   printf("This triangle is an equilateral triangle.");
  if(a==b || b==c || a==c)
   printf("This triangle is an isosceles triangle. ");
  if(a!=b && b!=c && a!=c)
   printf("This triangle is a scalene triangle. ");
  if(a>b && a>c)
  {
   if(a*a==(b*b)+(c*c))
     printf("This triangle is a right angled triangle. ");
  }
  else if(b>a && b>c)
  {
   if(b*b==(a*a)+(c*c))
     printf("This triangle is a right angled triangle. ");
  }
  else
  {
   if(c*c==(a*a)+(b*b))
     printf("This triangle is a right angled triangle. ");
  }
 }
  else
   printf("These are the sides of a invalid triangle.");
}

output:-

8 comments:

  1. on a=3
    b=3
    c=3
    out put is wronge

    ReplyDelete
  2. if(a==b && b==c)
    printf("This triangle is an equilateral triangle.");
    never be true in your code

    ReplyDelete
    Replies
    1. Sorry!! its my mistake and thanks to remind me about my mistake .....
      I m going to make it right ....

      Delete
  3. but if we give input as 5,4,3 it will take it as scalene triangle..then how i will get right angled triangle...

    ReplyDelete
    Replies
    1. Plz, compile it on your TurboC compiler. It will definitely give the right result. Because, triangle with sides 5,4,3 is both scalene and right angled triangle and this program tells this in this code.

      Delete
  4. TRY This, it'll never fail... no matter if you enter +ve, -ve or even 0 values.
    here's the code i have written-

    # include
    int main()
    {
    float a,b,c,s;
    printf("enter the sides of the triangle\n");
    scanf("%f%f%f",&a,&b,&c);
    if((a!=0&&a>0)&&(b!=0&&b>0)&&(c!=0&&c>0))
    {


    if(a>b&&a>c)
    {
    s=b+c;
    if(s>a)
    {
    if(b==c)
    {
    if(b*b+c*c==a*a)
    printf("right angeled & isosceles tri");
    else
    printf("isosceles tri");
    }

    else if(b!=c)
    {
    if(b*b+c*c==a*a)
    printf("right angeled & scelene tri");
    else
    printf("scelene tri");
    }



    }
    else
    printf("invalid tri");
    }

    else if(b>a&&b>c)
    {
    s=a+c;
    if(s>b)
    {
    if(a==c)
    {
    if(a*a+c*c==b*b)
    printf("right angeled & isosceles tri");
    else
    printf("isosceles tri");

    }

    else if(a!=c)
    {
    if(a*a+c*c==b*b)
    printf("right angeled & scelene tri");
    else
    printf("scelene tri");
    }




    }
    else
    printf("invalid tri");
    }
    else if(c>a&&c>b)
    {
    s=a+b;
    if(s>c)
    {
    if(a==b)
    {
    if(a*a+b*b==c*c)
    printf("right angeled & isosceles tri");
    else
    printf("isosceles tri");
    }

    else if(a!=b)
    {
    if(a*a+b*b==c*c)
    printf("right angeled & scelene tri");
    else
    printf("scelene tri");
    }





    }
    else
    printf("invalid tri");

    }
    else if((a==b)&&(a==c)&&(b==c))
    printf("equilateral tri");
    else if((a>b&&a==c)||(b>c&&b==a)||(c>a&&c==b))
    printf("isosceles tri");
    else
    printf("invalid tri");

    }
    else
    printf("tri doesn't exist");
    return 0;

    }





    try with any random combination of a,b and c
    i hope this helps





    ReplyDelete
  5. on putting 3 values 5,5,5, the output is : This triangle is an equilateral triangle.This triangle is an isosceles triangle.

    ReplyDelete
  6. i saw the code, the logic is correct but why on putting 3 same values it is saying This triangle is an equilateral triangle.This triangle is an isosceles triangle. i can't understand

    ReplyDelete