Write a program to find all the triangles with integer side lengths and a user s
ID: 3786139 • Letter: W
Question
Write a program to find all the triangles with integer side lengths and a user specified perimeter. Perimeter is the sum of all the sides of the triangle. Integers a, b and c form a triangle if the sum of the lengths of any two sides is greater than the third side. Your program should find all the triples a, b and c where a + b + c is user specified perimeter value. Print each triple only once in increasing order. For example, 3, 4 and 5 forms a triple with perimeter 12. Only print 3 4 5 instead of printing all the permutations of the triple.
Output example:
Please Enter Perimeter: 24
Traingles with perimeter 24
2 11 11
3 10 11
4 9 11
4 10 10
5 8 11
5 9 10
6 7 11
6 8 10
6 9 9
7 7 10
7 8 9
8 8 8
Thank you in advance!
Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
cout<<"Please Enter Perimeter:";
int perimeter;
cin>>perimeter;
int i,j,k;
for(i=1;i<perimeter;i++)
{
for(j=i;j<perimeter;j++)
{
k=perimeter-i-j;
if(k<=0||k<j)break;
if(i+j>k&&j+k>i&&i+k>j)
{
cout<<i<<" "<<j<<" "<<k<<" ";
}
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.