Algorithm and problem solving with C

Requirement
You are to get 3 sides of a triangle and validate if the triangle is valid, if it is valid your program needs to find out if the triangle is equilateral, isoceles or scalene.

Algorithm

prompt "Enter 3 integers";
get side1, side2, side3;

//triangle validation function
if !(side1 + side2 > side3):
set test_result = 1

else 
if !(side2 + side3 > side1):
set test_result=1

else 
if!(side1 + side3 > side2):
else set test_result = 0;
endif
endif
endif
endif

if (test_result=0)
display "Invalid triangle"
//equilateral (all sides equal)
else
if (side1 == side2 && side2 == side3):
display "Equilateral triangle";

else
//isosceles (at least 2 sides are equal)
if (side1 == side2 || side1 == side3 || side2 == side3):
display "Isosceles triangle";

//scalene (anything else)
else
 display "Scalene triangle"
 
endif
endif
endif
endif

The program in c. You can use the onlinegdb.com to validate the result

#include 
#include 

bool Triangle_Validation(float,float,float);

bool Triangle_Validation(float side1,float side2, float side3){
        bool triangle;
        if (!(side1+side2 > side3)){
            triangle = 0;
        }
        else if (!(side2+side3 > side1)){
            triangle = 0;
        }
        else if (!(side1+side3 > side2)){
            triangle = 0;
        }
        else {
            triangle = 1;
        }
    return triangle;
    }

int main()
{
    float side1,side2,side3;
    bool test_result;
    printf("Enter the values of 3 sides of a triangle:");
    scanf("%f%f%f",&side1,&side2,&side3);
    
    test_result = Triangle_Validation(side1,side2,side3);
    if (test_result == 0){
        printf("Invalid triangle");
    }
    else if (side1==side2 && side2==side3 && side1==side3){
        printf("Equilateral Triangle"); //all sides equal
    }
    else if (side1==side2 || side2==side3 || side1==side3){
        printf("Isoceles Triangle"); //at least two sides equal
    }
    else {
        printf("Scalene Triangle"); //use 5 4 3 to test, pythogarean type of triangle
    }

    return 0;
}
Advertisements
This entry was posted in General stuffs and tagged , . Bookmark the permalink.

One Response to Algorithm and problem solving with C

  1. cyruslab says:

    wordpress removes all tags, should have included the stdio.h and stdbool.h

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s