forked from CPRF-Session2/Assignment5
/
three.c
66 lines (58 loc) · 1.21 KB
/
three.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* Can Parlar - scans 3 numbers and checks if they can make a valid triangleand what will be the type of the triangle */
#include <stdio.h>
int isTriangle(int a, int b, int c) {
int valid;
if (a+b>c&&c+b>a&&a+c>b) {
valid = 1;}
else {
valid = 0;}
return valid;
}
int triangleType(int a, int b, int c) {
int type;
if (a==b&&b==c) {
type = 1;}
if (a==b&&a!=c||c==b&&b!=a||a==c&&a!=b) {
type = 2;}
if (a!=b&&b!=c&&c!=a) {
type = 3;}
return type;
}
int sanitizedInput() {
char line [50];
int num;
while (1) {
printf("\nEnter a number:");
fgets(line, sizeof(line), stdin);
if(sscanf(line, "%d", &num)) {
return num;
break;}
printf("You did not enter a number!\n");}
}
int main() {
int a;
int b;
int c;
int valid;
int type;
int num;
num = sanitizedInput();
a = num;
num = sanitizedInput();
b = num;
num = sanitizedInput();
c = num;
valid = isTriangle(a, b, c);
type = triangleType(a, b, c);
if (valid == 0) {
printf("Your triangle is not valid!\n");}
if (valid == 1) {
printf("Your triangle is valid!\n");}
if (type == 1) {
printf("Your triangle is equilateral!\n");}
if (type == 2) {
printf("Your triangle is isoceles!\n");}
if (type == 3) {
printf("Your triangle is scalene!\n");}
return 0;
}