Table of contents
1.
Introduction
1.1.
Problem Statement
1.2.
Algorithm
1.3.
Implementation
1.4.
Complexity Analysis 
2.
Frequently Asked Questions
2.1.
What are the operators?
2.2.
Why are logical operators used?
2.3.
Which is logical or operator?
2.4.
What is printf & scanf in C?
2.5.
Differentiate between ++i and i++ in C?
3.
Conclusion
Last Updated: Mar 27, 2024
Easy

C Program to check a triangle is a equilateral, isosceles or scalene

Author Amit Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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;
}
You can also try this code with Online C Compiler
Run Code

 

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.

Conclusion

In this article, we have studied how to C Program to check if a triangle is equilateral, isosceles or scalene. We achieved this task by using logical operators and conditional statements. We hope that this article has provided you with the help to enhance your knowledge regarding programming in C language and if you would like to learn more, check out our articles on jump statements in C/C++Short int in C Programming, bitwise operators in C/C++ and C vs C++.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available, Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Merry Learning!

Live masterclass