Introduction
In this article, we will write a C program to check if a triangle is equilateral, isosceles, or scalene. To reach this goal, we need to have information about the logical operators and if-else statements in C.
The if-else statement in C is used to perform the operations based on some specific condition. The functions specified in the if block are executed if and only if the given situation is true.
A triangle is a closed shape that consists of three sides with three angles. Based on the three sides, there are three types of the triangle −
- Equilateral triangle: All three sides of the triangle are equal.
- Isosceles triangle: All two sides of the triangle are equal.
- Scalene triangle: No sides of the triangle are equal.
Problem Statement
The problem statement is that we are given an array of ‘n’ elements, and we have to count the number of pairs in the array whose products exist in the array.
Sample Input
5 6 7 |
Sample Output:
All the sides of this triangle are unequal. So, the triangle is scalene. |
Explanation:
A triangle in which all sides are equal is known as an equilateral triangle. A triangle in which only two sides are equal is known as an isosceles triangle. A triangle in which all sides are different is known as a scalene triangle. Here, all sides are different. That is why it is a scalene triangle. |
Algorithm
Step 1: Declaration of three sides of the triangle.
Step 2: Taking three sides as input from the user at run time.
Now, check the conditions in steps 3 to step 5 to figure out the type of triangle.
Step 3: If (side_1 == side_2 && side_2 == side_3)
Then go to step 6.
Step 4: If (side_1 == side_2 || side_2 == side_3 || side_3 == side_1)
then go to Step 7.
Step 5: Else
Then go to step 8.
Step 6: If the triangle is equilateral. Print, "All the sides of this triangle are equal. So, the triangle is equilateral.".
Step 7: If the triangle is isosceles. Print, “Only two sides of this triangle are equal. So, the triangle is isosceles.".
Step 8: If the triangle is scalene. Print, "All the sides of this triangle are unequal. So, the triangle is scalene.".
Also Read, Binary to Hex Converter,C Static Function
Implementation
#include<stdio.h>
int main()
{
int side_1, side_2, side_3;
printf("Enter the sides of the triangle:");
scanf("%d%d%d", &side_1, &side_2, &side_3);
if(side_1 == side_2 && side_2 == side_3)
{
printf("All the sides of this triangle are equal. So, the triangle is equilateral.\n");
}
else if(side_1 == side_2 || side_2 == side_3 || side_3 == side_1)
{
printf("Only two sides of this triangle are equal. So, the triangle is isosceles.\n");
}
else
{
printf("All the sides of this triangle are unequal. So, the triangle is scalene.\n");
}
return 0;
}
Case 1: Check if the triangle is Scalene by giving unequal sides as input.
Output:
Case 2: Check if the triangle is Isosceles by giving only two equal sides as input.
Output:
Case 3: Check if the triangle is Equilateral by giving all equal sides as input.
Output:
You can implement this code on an online c compiler for better understanding.
Complexity Analysis
Time Complexity: O(1)
Space Complexity: Constant space.
You can also read about dynamic array in c and Tribonacci Series.
Frequently Asked Questions
What are the operators?
In mathematics and generally sometimes in computer programming, an operator is a character that represents an action; for example, x is an arithmetic operator that represents multiplication. In computer programs, one of the most familiar sets of operators, the Boolean operators, is used to work with true/false values.
Why are logical operators used?
The logical operators are a type of operators that are primarily used in the evaluation of expression so that a decision can be made. These operators allow the assessment and manipulation of specific bits within the integer. This operator returns true if all relational statements combined with && are true; else, it returns false.
Which is logical or operator?
The logical OR operator or ( || ) returns the boolean value true if either or both operands are true, and if they are both false, it returns false otherwise. The operands are implicitly converted to type bool before evaluation, and the result is of type bool. Logical OR has left-to-right associativity.
What is printf & scanf in C?
printf() and scanf() are library functions that are inbuilt in the C programming language that allows us to perform formatted input and formatted output functions. Both of these functions are defined and declared in stdio.h header file. The 'f' in printf and scanf stands for 'formatted'.
Differentiate between ++i and i++ in C?
'i++' means that when the code is executing, it will first read it and do the increment (i = i + 1) after it has been read. ‘++i’ means that when the code is executing, it will first do i=i+1 and then read it.