Write a program to find the area of any triangle


Default
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
61
62
63
64
// Write a program to find the area of triangle
// Ex: opt:1,base:3,height:4->Area:6; opt:2,a:2,b:3;c:4->Area:2.90
// Author: AimForJob.com
 
#include
#include
int main(){
    int opt;
    double area;
    printf("(1)Area of triangle using base-height\n");
    printf("(2)Area of triangle using three sides\n");
    printf("Please select your choice:");
    scanf("%d",&opt);
    while(opt<=0){
        printf("Please select either 1 or 2:");
        scanf("%d",&opt);      
    }
    if(opt==1){
        double b,h;
        printf("Enter the Base of triangle:");
        scanf("%lf",&b);
        while(b<=0){
            printf("Base of triangle can't be negative or zero");
            printf("Enter the Base of triangle:");
            scanf("%lf",&b);      
        }
        printf("Enter the height of triangle:");
        scanf("%lf",&h);
while(b<=0){
            printf("Height of triangle can't be negative or zero");
            printf("Enter the height of triangle:");
            scanf("%lf",&h);      
        }      
        area = (b*h)/2.0;
    }
    else if(opt==2){
        double a,b,c,s;
        printf("Enter the triangle side1:");
        scanf("%lf",&a);
        while(a<=0){
            printf("Side of triangle can't be negative or zero");      
            printf("Enter the triangle side1:");
            scanf("%lf",&a);  
        }
        printf("Enter the triangle side2:");
        scanf("%lf",&b);
        while(b<=0){
            printf("Side of triangle can't be negative or zero");      
            printf("Enter the triangle side2:");
            scanf("%lf",&b);  
        }
        printf("Enter the triangle side3:");
        scanf("%lf",&c);
        while(c<=0){
   printf("Side of triangle can't be negative or zero");      
            printf("Enter the triangle side3:");
            scanf("%lf",&c);  
        }
        s = (a+b+c)/2.0;
        area = sqrt(s*(s-a)*(s-b)*(s-c));
    }
    printf("Area of triangle: %lf",area);
    getch();
}

No comments:

Post a Comment