[text] lol

Viewer

  1. //Purpose: This program takes three positive intergers and outputs the type of triangle 
  2. //it is as well as the area of the triangle.
  3. //Name: Daniel Smith 
  4. //Section - 20737,20738
  5. //Lab 1B
  6.  
  7. #include <stdio.h>
  8. #include <math.h>
  9. #include <iostream>
  10. using std::cout;
  11. using std::endl;
  12. using std::cin;
  13.  
  14. int is_triangle(int a, int b, int c) // function to check if it is a valid triangle
  15. {
  16.         // Property used : Sum of two sides is always greater than the third side
  17.         if ((a + b) <= c)
  18.         {
  19.                 return 0;
  20.         }
  21.         if ((b + c) <= a)
  22.         {
  23.                 return 0;
  24.         }
  25.         if ((c + a) <= b)
  26.         {
  27.                 return 0;
  28.         }
  29.         return 1;
  30. }
  31. float area_triangle(int a, int b, int c) // function to calculate area
  32. {
  33.         float s = (a + b + c) / 2;
  34.         float area = sqrt((s) * (s - a) * (s - b) * (s - c));
  35.         return area;
  36. }
  37. int main()
  38. {
  39.         int a, b, c;
  40.         printf("Enter three positive integers : ");
  41.         scanf_s("%d %d %d", &a, &b, &c);
  42.         int ans = is_triangle(a, b, c);
  43.         if (!ans)
  44.         {
  45.                 printf("The integers %d, %d and %d are not valid sides of triangle", a, b, c);
  46.         }
  47.         if (ans)
  48.         {
  49.                 if ((a == b) && (a == c))
  50.                 {
  51.                         printf("It is an equilateral triangle\n");
  52.                 }
  53.                 else if (a != b && b != c && c != a)
  54.                 {
  55.                         printf("It is a scalene triangle\n");
  56.                 }
  57.                 else
  58.                 {
  59.                         printf("It is an isosceles triangle\n");
  60.                 }
  61.                 float area = area_triangle(a, b, c);
  62.                 printf("Area of the triangle is %f", area);
  63.  
  64.  
  65.         }
  66.         {
  67.                 char choice;
  68.                 while (true)
  69.                 {
  70.  
  71.                         cout << "\nWould you like to perform other calculation?(Y/N)" << endl;
  72.                         cin >> choice;
  73.                         if (choice == 'Y' || choice == 'y') {
  74.                                 return main();
  75.                         }
  76.                         else if (choice == 'N' || choice == 'n') {
  77.                                 return false;
  78.                         }
  79.                 }
  80.  
  81.         }
  82.  
  83.         return 0;
  84. }

Editor

You can edit this paste and save as new:


File Description
  • lol
  • Paste Code
  • 01 Mar-2021
  • 1.65 Kb
You can Share it: