Write a basic c++ program to check whether a triangle is valid
or not if the three sides are given:
A triangle is valid if the sum of its two sides is greater than the
third side.
Let’s say that a, b, c is the sides of the triangle. A valid
triangle must satisfy all these conditions:
a + b > c
a + c > b
b + c > a
(2 points) Generate random numbers 1-15 inclusive for each of a, b
and c.
Display if the three sides do not make a valid triangle.
(2 points) If the three sides do make a valid triangle, determine
whether the triangle is equilateral, scalene or isosceles:
- Equilateral triangle is a triangle in which all three sides are
equal.
- Isosceles triangle is a triangle that has two sides of equal
length.
- Scalene triangle is a triangle that has three unequal
sides.
(2 points) Loop 10 times, each time generate three random sides
1-15 inclusive
Print whether the numbers make an invalid, an equilateral,
isosceles or scalene triangle.
(2 points) Print the sum of all a, all b and all c and the
resulting triangle.
Print how many of each at the end.
(2 points) Your name, correct indentation, and no redundant
(unnecessary) code.
14 2 12 not a valid triangle
11 1 9 not a valid triangle
8 1 10 not a valid triangle
4 5 9 not a valid triangle
14 13 6 is scalene
11 10 6 is scalene
10 8 2 not a valid triangle
7 7 7 is equilateral
1 11 13 not a valid triangle
14 5 14 is isosceles
Total a = 94, b = 63, c = 88
94 63 88 is scalene
6 not valid
1 equilateral
1 isosceles
3 scalene
The Code is well explained in comments itself
#include <iostream>
#include <stdlib.h>
//importing necessary libraries
using namespace std;
int equi=0,scal=0,nottrain=0,isoc=0;//global varaible for counting traigle types
string traingleType(int a,int b,int c) //function which returns traingle type
{
if(a==b && b==c && a==c) {equi++; return " is equilateral";} //condition for equilateral traingle
else if(a==b || b==c || a==c) {isoc++;return " is isosceles ";} //condition for isoceles traingle
else{scal++; return " is scalene ";} //condition for scalene traingle
}
int main()
{
int A=0,B=0,C=0,a,b,c; // declaring varaibles for total sum sides and separate sides
for (int i=0;i<10;i++)
{
a=rand() % 15 + 1; // random number for side 1 between 1 to 15 inclusive
b=rand() % 15 + 1; // random number for side 2 between 1 to 15 inclusive
c=rand() % 15 + 1; // random number for side 2 between 1 to 15 inclusive
A=A+a;B=B+b;C=C+c; //for sum of all sides
if(a+b>c && b+c>a && a+c>b) // condition for valid traingle
{
cout<<a<<" "<<b<<" "<<c<<traingleType(a,b,c)<<"\n"; // printint traingle type
}
else{
nottrain++; //updating not valid traingle count
cout<<a<<" "<<b<<" "<<c<<" not a valid triangle"<<"\n";
}
}
cout<<"\nTotal a = "<<A<<" b = "<<B<<" c = "<<c<<"\n";
//printing sum of all sides
if(A+B>C && B+C>A && A+C>B) // condition for valid traingle
{
cout<<A<<" "<<B<<" "<<C<<traingleType(a,b,c)<<"\n";
}
else{
cout<<A<<" "<<B<<" "<<C<<" not a valid triangle"<<"\n";
}
cout <<nottrain<<" not valid"<<"\n";
cout <<equi<<" equilateral"<<"\n";
cout <<isoc<<" isoceles"<<"\n";
cout <<scal<<" scalene"<<"\n";
//printing respective counts for traingle types
return 0;
}
In case of any doubts/queries or if you want something
else/improvement in the answer please write them in the
comments.
Also, provide your valuable feedback.
Thanks
Get Answers For Free
Most questions answered within 1 hours.