Thursday, 14 March 2019

C program to find whether a triangle can be formed or not

C program to find whether a triangle can be formed or not and also display which type of triangle.

C program to find whether a triangle can be formed or not


In this tutorial of C program we will find if a triangle can be formed or not using three sides which will be taken from the user.


Steps to follow to make the C program


The length of three sides are taken as input from the user.


Write a C program to find whether a triangle can be formed or not. 


If not then display “This Triangle is NOT possible.” 


C program to find if triangle can be formed or not



If the triangle can be formed then check whether the triangle formed is equilateral, isosceles, scalene or a right-angled triangle. 



In general mathematics, 

A triangle has three sides, which can be different from each other in length or same depending upon what type of triangle they are.


From the properties of triangle we can say that a triangle can be formed only when sum of two sides are greater than the third side.
That means A+B>C


In Isosceles triangle, the sum of two sides are equal to the third side.
That means A+B=C


In a right angle triangle, from pythgoras theorem we can say sum of the square of perpendicular and base is equal to the square of hypotenuse.
That means A^2+B^2=C^2


In a equilateral triangle all the sides all the sides are equal in length and all the angles make 60 degree.
That means  A=B=C

In a scalene triangle all the sides are unequal and hence all the sides must be different from each other.
A!=B!=C


C Program to Find if a Triangle can be Formed or Not given Three sides from the User.



#include<stdio.h>

#include<conio.h>

void main()
{
    int a,b,c; 

    clrscr(); 

    printf("Enter the sides of triangle : ");
    scanf("%d %d %d",&a, &b, &c); 

    if(a+b<=c || a+c<=b || b+c<=a)
        printf("Triangle is not possible");
    else if (a*a+b*b==c*c)
        printf("Right-angle Triangle");
    else if (a+b==c || a+c==b || b+c==a)
        printf("Isosceles Triangle");
     else if(a==b && b==c && c==a)
        printf("Equilateral Triangle");
    else
       printf("Scalene Triangle");

    getch();
 }





Method 2 of Doing the C Program



#include<stdio.h>

#include<conio.h>

void main()
{
    int a,b,c; 

    clrscr(); 

    printf("Enter the sides of triangle : ");
    scanf("%d %d %d",&a, &b, &c); 

    if(a<(b+c)&&b<(a+c)&&c<(a+b))
    {
        if(a==b&&a==c&&b==c)
        printf("Equilateral Triangle");
          else if(a==b||a==c||b==c)
          printf("Isosceles Triangle");
          else  
    if((a*a)==(b*b)+(c*c)||(b*b)==(a*a)+(c*c)||(c*c)==(a*a)+(b*b))
        printf("Right-angle Triangle");
        else if(a!=b&&a!=c&&b!=c)
        printf("Scalene Triangle");
    }
    else
    printf("Triangle is not possible");
}

    getch();
 }


Input : 10, 4, 6

Output : Triangle is not possible

No comments:

Post a Comment