Go to Top

Thursday 11 August 2011

To find out whether a triangle is right angled or not

Question 43 : Write a program in Java to find out whether a triangle is right angled or not by taking the sides of the triangle as inputs by the user.


Java Program :



import java.io.*;
class right_angled
{
 static void main()throws IOException
 {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     int a,b,c;
     System.out.println("Enter the value of the sides\n");
     System.out.print("Side 1 : ");
     a=Integer.parseInt(br.readLine());
     System.out.print("Side 2 : ");
     b=Integer.parseInt(br.readLine());
     System.out.print("Side 3 : ");
     c=Integer.parseInt(br.readLine());
     System.out.print("\n");
     if(a>b&&a>c)
     {
         if((a*a)==(b*b)+(c*c))
         System.out.println("It is a right-angled triangle");
         else 
         System.out.println("It is not a right-angled triangle");
        }
        if(b>c&&b>a)
        {
            if((b*b)==(c*c)+(a*a))
            System.out.println("It is a right-angled triangle");
            else
            System.out.println("It is not a right-angled triangle");
        }
        if(c>a&&c>b)
        {
            if((c*c)==(a*a)+(b*b))
            System.out.println("It is a right-angled triangle");
            else
            System.out.println("It is not a right-angled triangle");
        }
    }
}

24 comments:

  1. Thankzz a lot..it helped me out of my prob..!!!

    ReplyDelete
  2. can we do this program without using Buffered

    ReplyDelete
    Replies
    1. yes, it can be done by function argument too.

      Delete
  3. if we can than can you please show how we do that

    ReplyDelete
    Replies
    1. Yes patel we can.
      Just we have to make only few changes. Like if u want input from scanner classes so you just study its format and you can also do it by initializing th value of sides on the top of the program

      Delete
  4. what if a=4, b=6 and c=12 ?
    The program will fail

    ReplyDelete
    Replies
    1. Yes but because for making the triangle every side should be less than the sum of other two side but in question for checking the triangle is not given. So that's not a problem one Condition can be added before that its just my suggestion or request.
      if(a<b+c && b<a+c && c<a+b)

      if any query my id is anees.itlucknow@gmail.com

      Delete
  5. u guys r smart im not so idek what u guys r talking abt i thought this was a website for homework answers :T smh

    ReplyDelete

ShareThis