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");
        }
    }
}

1 comment:

ShareThis