◦ Define an enumeration type, TriangleType, that has the values scalene, isosceles , equilateral, and notriangle.
◦ Write a function, triangleShape, that takes as parameters three numbers, each of which represents the length of a side of the triangle. The function should return the type of the triangle. (Note: In a triangle, the sum of the lengths of any two sides is greater than the length of the third side.)
◦ Write a program that prompts the user to input the length of the sides of a triangle and outputs the type of the triangle.
#include<iostream>
using namespace std;
enum TriangleType {scalene, isosceles, equilateral, noTriangle};
TriangleType triangleShape(int side1,int side2,int side3){
if(side1<=0 || side2<=0 || side3<=0){
return noTriangle;
}
//If all sides are equal
if(side1==side2 && side2==side3){
return equilateral;
}
//If any two sides are equal
else if(side1==side2 || side1==side3 || side2==side3){
return isosceles;
}
else{
//If none sides are equal
return scalene;
}
}
int main(){
int side1;
int side2;
int side3;
cout<<"Enter side 1: ";
cin>>side1;
cout<<"Enter side 2: ";
cin>>side2;
cout<<"Enter side 3: ";
cin>>side3;
if(triangleShape(side1,side2,side3)==scalene){
cout<<"Triangle is scalene\n\n";
}
if(triangleShape(side1,side2,side3)==isosceles){
cout<<"Triangle is isosceles\n\n";
}
if(triangleShape(side1,side2,side3)==equilateral){
cout<<"Triangle is equilateral\n\n";
}
if(triangleShape(side1,side2,side3)==noTriangle){
cout<<"It is not a triangle\n\n";
}
system("pause");
return 0;
}
Comments
Leave a comment