Whether the triangle is equilateral, scalene or isosceles
C program to find out whether the triangle is equilateral, scalene or isosceles.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int side1,side2,side3;
printf("Enter the length of the first side\t: ");
scanf("%d",&side1);
printf("Enter the length of the second side\t: ");
scanf("%d",&side2);
printf("Enter the length of the third side\t: ");
scanf("%d",&side3);
if((side1==side2) && (side1==side3))
{
printf("\nEquilateral triangle");
}
else if((side1==side2)||(side1==side3)||(side2==side3))
{
printf("\nThe triangle is isosceles");
}
else
{
printf("\nThe triangle is scalene");
}
getch();
}
Output
********** Run1 **********
Enter the length of the first side : 12
Enter the length of the second side : 12
Enter the length of the third side : 23
The triangle is isosceles
********** Run2 **********
Enter the length of the first side : 12
Enter the length of the second side : 23
Enter the length of the third side : 34
The triangle is scalene
********** Run3 **********
Enter the length of the first side : 12
Enter the length of the second side : 12
Enter the length of the third side : 12
Equilateral triangle
Explanation
Let, a, b, and c be three sides of the triangle
A triangle is said to be Equilateral Triangle, if all its sides are equal. So, the triangle is equilateral only if a == b == c.
A triangle is said to be Isosceles Triangle, if its two sides are equal. So, the triangle is isosceles if either a == b or a == c or b == c.
A triangle is said to be Scalene Triangle, if none of its sides are equal.
In above program, the three sides are taken as an input in the variables named 'side1', 'side2' and 'side3' respectively.
For equilateral triangle, condition used is:
If all the three sides are equal, then the above condition evaluates to true and the triangle is equilateral.
If condition evaluates to false, then it is checked for isosceles triangle.
If any of the two sides are equal, then the above condition evaluates to true and the triangle is isosceles.
If the condition evaluates to false, then it is a scalene triangle and no condition check is required. So, for this purpose, else block is used.
In above program, the three sides are taken as an input in the variables named 'side1', 'side2' and 'side3' respectively.
For equilateral triangle, condition used is:
if((side1==side2) && (side1==side3))
If all the three sides are equal, then the above condition evaluates to true and the triangle is equilateral.
If condition evaluates to false, then it is checked for isosceles triangle.
else if((side1==side2)||(side1==side3)||(side2==side3))
If any of the two sides are equal, then the above condition evaluates to true and the triangle is isosceles.
If the condition evaluates to false, then it is a scalene triangle and no condition check is required. So, for this purpose, else block is used.