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:
- The program starts by including the necessary header file stdio.h, which provides input/output functions like printf and scanf.
- In the main function, we declare the variables angle1, angle2, and angle3 to store the three angles of the triangle.
- The printf function is used to prompt the user to enter the three angles.
- The scanf function is used to read the three angles entered by the user and store them in the corresponding variables.
- We then use an if-else statement to determine the type of triangle based on the angles.
- 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.
- If any two angles are equal, we conclude that it is an isosceles triangle, as an isosceles triangle has two equal angles.
- If none of the above conditions are met, we conclude that it is a scalene triangle, as a scalene triangle has no equal angles.
- Finally, we use the printf function to print the type of triangle detected.
- 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:
- The program starts by importing the java.util.Scanner class, which allows us to read input from the user.
- The TriangleType class is defined, which contains the main method where the program execution begins.
- Inside the main method, three integer variables angle1, angle2, and angle3 are declared to store the input angles of the triangle.
- A Scanner object named scanner is created to read input from the user.
- The program prompts the user to enter three angles of a triangle using System.out.print(“Enter three angles of a triangle: “).
- 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.
- 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.”
- The program execution ends, and the program terminates.
Java Output
Enter three angles of a triangle: 40
80
50
It is a scalene triangle.