Go to Top

Wednesday 6 April 2011

To classify triangles on basis of their sides

Question 6 : Write a program in Java to classify a triangle whether it is equilateral, isosceles, or scalene on the basis of the values of the three sides given by the user.


Java Program :


public class triangles
{
 public static void main(int a,int b,int c)
 {
    if (a==b && b==c)
    System.out.println("The triangle with sides "+a+","+b+","+c+" is an equilateral one");
    if ((a==b && b!=c && a!=c) || (b==c && b!=a && c!=a) || (c==a & c!=b && a!=b))
    System.out.println("The triangle with sides "+a+","+b+","+c+" is an isosceles one");
    if (a!=b && a!=c && b!=c)
    System.out.println("The triangle with sides "+a+","+b+","+c+" is a scalene one");
}
}

Input : 


(i) a=10
    b=10
    c=10

(ii) a=10
     b=10
     c=20

(iii) a=10
      b=15
      c=20

Output : 


(i) The triangle with sides 10,10,10 is an equilateral one


(ii) The triangle with sides 10,10,20 is an isosceles one


(iii) The triangle with sides 10,15,20 is a scalene one

19 comments:

  1. Case ii (a=10,b=10,c=20) can not be an isosceles triangle. To be a triangle, addition of any two sides must be greater that the third side.

    ReplyDelete
  2. import java .util.*;
    public class Triangle
    {
    public static void main()
    {
    Scanner sc= new Scanner(System.in);
    System.out.println("enter the side ab");
    int ab=sc.nextInt();
    System.out.println("enter the side bc");
    int bc=sc.nextInt();
    System.out.println("enter the side ac");
    int ac=sc.nextInt();

    if( ab == bc)
    {
    if(bc==ac)
    {
    System.out.println("eqilateral");

    }
    }
    if( ab ==bc)
    {
    if(bc>ac)
    {
    System.out.println("isoceles");
    }
    }
    if( ab != bc)
    {
    if(bc != ac)
    {
    System.out.println("scalen");
    }
    }
    }
    }

    ReplyDelete
  3. import java .util.*;
    class Triangle
    {
    public static void main(String args[])
    {
    Scanner sc= new Scanner(System.in);
    System.out.println("enter the side ab");
    int ab=sc.nextInt();
    System.out.println("enter the side bc");
    int bc=sc.nextInt();
    System.out.println("enter the side ac");
    int ac=sc.nextInt();

    if( a==b&& b==c&&c==a)
    System.out.println("equilateral");
    else if (a==b||b==c||c==a)
    System.out.println("isosceles");
    else
    System.out.println("Scalene");
    }
    }

    ReplyDelete
  4. Saale bahancho bhaadiya program hai bahas ki chuut

    ReplyDelete
  5. On basis of their angle and side write a program to find type of triangle

    ReplyDelete

ShareThis