Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Program to determines equilateral, isosceles, or scalene triangle

Write a program that takes three angles of a triangle as input and determines whether it is an equilateral, isosceles, or scalene triangle.

Program in C

C
#include <stdio.h>

int main() {
    int angle1, angle2, angle3;

    // Getting input from the user
    printf("Enter three angles of a triangle: ");
    scanf("%d %d %d", &angle1, &angle2, &angle3);

    // Checking the type of triangle
    if (angle1 == angle2 && angle2 == angle3) {
        printf("It is an equilateral triangle.\n");
    } else if (angle1 == angle2 || angle2 == angle3 || angle1 == angle3) {
        printf("It is an isosceles triangle.\n");
    } else {
        printf("It is a scalene triangle.\n");
    }

    return 0;
}

Explanation:

  1. The program starts by including the necessary header file stdio.h, which provides input/output functions like printf and scanf.
  2. In the main function, we declare the variables angle1, angle2, and angle3 to store the three angles of the triangle.
  3. The printf function is used to prompt the user to enter the three angles.
  4. The scanf function is used to read the three angles entered by the user and store them in the corresponding variables.
  5. We then use an if-else statement to determine the type of triangle based on the angles.
  6. If all three angles are equal, we conclude that it is an equilateral triangle, as all sides and angles of an equilateral triangle are equal.
  7. If any two angles are equal, we conclude that it is an isosceles triangle, as an isosceles triangle has two equal angles.
  8. If none of the above conditions are met, we conclude that it is a scalene triangle, as a scalene triangle has no equal angles.
  9. Finally, we use the printf function to print the type of triangle detected.
  10. We return 0 to indicate successful execution of the program.

Output:

Output
Enter three angles of a triangle: 40
20
60
It is a scalene triangle.

Program in Java

Java
import java.util.Scanner;

public class TriangleType {
    public static void main(String[] args) {
        int angle1, angle2, angle3;

        // Getting input from the user
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter three angles of a triangle: ");
        angle1 = scanner.nextInt();
        angle2 = scanner.nextInt();
        angle3 = scanner.nextInt();

        // Checking the type of triangle
        if (angle1 == angle2 && angle2 == angle3) {
            System.out.println("It is an equilateral triangle.");
        } else if (angle1 == angle2 || angle2 == angle3 || angle1 == angle3) {
            System.out.println("It is an isosceles triangle.");
        } else {
            System.out.println("It is a scalene triangle.");
        }
    }
}

Explanation:

  1. The program starts by importing the java.util.Scanner class, which allows us to read input from the user.
  2. The TriangleType class is defined, which contains the main method where the program execution begins.
  3. Inside the main method, three integer variables angle1, angle2, and angle3 are declared to store the input angles of the triangle.
  4. A Scanner object named scanner is created to read input from the user.
  5. The program prompts the user to enter three angles of a triangle using System.out.print(“Enter three angles of a triangle: “).
  6. The nextInt() method of the Scanner class is used to read integer values entered by the user, and the values are assigned to angle1, angle2, and angle3 variables.
  7. The program checks the type of triangle based on the input angles using conditional statements.
    • If angle1 is equal to angle2 and angle2 is equal to angle3, it means all three angles are equal, and the program prints “It is an equilateral triangle.”
    • If any two angles are equal (e.g., angle1 == angle2 or angle2 == angle3 or angle1 == angle3), it means the triangle is isosceles, and the program prints “It is an isosceles triangle.”
    • If none of the above conditions are true, it means all three angles are different, and the triangle is scalene. The program prints “It is a scalene triangle.”
  8. The program execution ends, and the program terminates.
Java Output
Enter three angles of a triangle: 40
80
50
It is a scalene triangle.