Created
November 21, 2020 07:59
-
-
Save atwayne/6417621c207503a1b367d382ff714b16 to your computer and use it in GitHub Desktop.
an example program to check type of triangle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
// check sideToCheck | |
int checkSide(int firstSide, int secondSide, int sideToCheck) | |
{ | |
if (sideToCheck > firstSide + secondSide) | |
{ | |
return 0; | |
} | |
if (sideToCheck < abs(firstSide - secondSide)) | |
{ | |
return 0; | |
} | |
return 1; | |
} | |
// check all three sides of a triangle | |
int checkSides(int x, int y, int z) | |
{ | |
return checkSide(x, y, z) && checkSide(x, z, y) && checkSide(y, z, x); | |
} | |
int isEquilateral(int x, int y, int z) | |
{ | |
return x == y && x == z; | |
} | |
int isIsosceles(int x, int y, int z) | |
{ | |
return x == y || x == z || y == z; | |
} | |
int isRightTriangle(int x, int y, int z) | |
{ | |
int powOfX = pow(x, 2); | |
int powOfY = pow(y, 2); | |
int powOfZ = pow(z, 2); | |
int maxSide = powOfX; | |
if (powOfY > maxSide) | |
{ | |
maxSide = powOfY; | |
} | |
if (powOfZ > maxSide) | |
{ | |
maxSide = powOfZ; | |
} | |
return maxSide * 2 == powOfX + powOfY + powOfZ; | |
} | |
int main() | |
{ | |
int x, y, z; | |
printf("Enter x,y,z:\n"); | |
scanf("%d%d%d", &x, &y, &z); | |
int isValidSide = checkSides(x, y, z); | |
if (!isValidSide) | |
{ | |
printf("Invalid Triangle!\n"); | |
return -1; | |
} | |
if (isIsosceles(x, y, z)) | |
{ | |
printf("Isosceles\n"); | |
if (isEquilateral(x, y, z)) | |
{ | |
printf("Equilateral\n"); | |
} | |
} | |
else if (isRightTriangle(x, y, z)) | |
{ | |
printf("RightTriangle\n"); | |
} | |
printf("End\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment