Saturday, December 8, 2018

C code review find type of triangle by its input values

This C code shows how to find type of triangle by its input values. And code is helpful while user is in between any algorithm-like manipulation task.

code:

//Tested OK on Code::Blocks IDE 17.12 & Cygwin+GCC tools.
//Program to find the type of the triangle made by i/p values of
//its coordinates of its vertices.

#include <stdio.h>
//#include <conio.h>
#include <math.h>
#include<stdlib.h>

void main()
{
    float x1,y1,x2,y2,x3,y3;
    float a,b,c,m1,m2,m3;
//    clrscr();
    printf("\nEnter coordinates of vertex A(x & y respectively):-  ");
    scanf("%f%f",&x1,&y1);
    printf("\nEnter coordinates of vertex B(x & y respectively):-  ");
    scanf("%f%f",&x2,&y2);
    printf("\nEnter coordinates of vertex C(x & y respectively):-  ");
    scanf("%f%f",&x3,&y3);
    if ((x1==x2 && x2==x3)||(y1==y2 && y2==y3))
    {
        printf("\nThese coordinates can't represent a triangle.");
        printf("\nA,B & C are colinear & thus constitute a line.");
        printf("\nHAVE A NICE DAY! BYE.");
        getchar();
        exit(0);
    }
    else
    {
        m1=(y2-y1)/(x2-x1);
        m2=(y3-y2)/(x3-x2);
        m3=(y3-y1)/(x3-x1);
    }
    if (m1==m2||m2==m3||m3==m1)
    {
        printf("\nThese coordinates can't represent a triangle.");
        printf("\nA,B & C are colinear & thus consitute a line.");
        printf("\nHAVE A NICE DAY! BYE.");
        getchar();
        exit(0);

    }
    a = sqrt(pow((x2-x3),2) + pow((y2-y3),2));
    b = sqrt(pow((x3-x1),2) + pow((y3-y1),2));
    c = sqrt(pow((x2-x1),2) + pow((y2-y1),2));
    printf("\nLength of side AB is = %f",c);
    printf("\nLength of side BC is = %f",a);
    printf("\nLength of side CA is = %f",b);
    if (a==b==c)
        printf("\nTriangle made by these vertices is an equilateral triangle.\n");
    else if (a==b||b==c||c==a)
    {
        if (a==b==c);
        else
            printf("\nTriangle made by these vertices is an isosceles triangle.\n");
    }
    else if (a!=b && b!=c && c!=a)
        printf("\nTriangle made by these vertices is a scalene triangle.\n");
    printf("\nHAVE A NICE DAY! BYE.\n");
    getchar();
}


//end of program

To see such C code reviews refer blog archieve at right side or click CCode lable for till posted posts list.

...till next post, bye-bye and take care.

No comments:

Post a Comment