Dev C Please Write a program to find all the triangles with

Dev C++, Please!

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 S forms a triple with perimeter 12. Only print 3 4 5 instead of printing all the permutations of the triple. Sample output for this recitation is as follows Enter perimeter 24 Triangles with perimeter 24

Solution

#include <bits/stdc++.h>
using namespace std;

int main(int argc, char const *argv[])
{
   /* code */
   int count = 0; // keep track of total no. of solutions
int peri;
cout<<\"Enter perimeter\ \";
cin>>peri;
cout<<\"Triangles with perimeter \"<<peri<<endl;
for (int a = 1;a<=peri;a++) { // ranges can be optimized
for (int b = a ;b<=peri;b++) {
if (a + b >= peri) {
break; // no solution
}

int c = peri - (a + b);

if (c < b) {
break; // no solution
}

if (a + b > c && a + c > b && b + c > a) {
cout<<a<<\" \"<< b<<\" \"<<c<<endl;// solution
count++;
}
}
}
   return 0;
}

=========================================

Output:

akshay@akshay-Inspiron-3537:~$ g++ tri.cpp
akshay@akshay-Inspiron-3537:~$ ./a.out
Enter perimeter
24
Triangles 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

Dev C++, Please! 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 th
Dev C++, Please! 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 th

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site