Skip to content
Home ยป C Program To Identify A Quadrilateral

C Program To Identify A Quadrilateral

    The program to identify a quadrilateral read inputs – length of sides and angles made by the quadrilateral shape and prints the shape name as output.

    We compiled the program using Dev-C++ compiler installed on a windows 7 64-bit system. You may try another standard C compiler, but make sure to modify the code according to your compiler specifications.

    You must be aware of following C programming concept to understand this example program.

    Before you practice this program, you must go through the concepts of quadrilateral shapes and their differences. See the problem definition section.

    Problem Definition

    A quadrilateral is a polygon shape that has 4 sides and corners that make 4 angles. The sum of those angles must be equal to 360 \hspace{2px} degrees. There are many types of quadrilateral shapes, you will find a brief description of each shape below.

    Square

    A square has 4 equal sides and 4 right angles at each corner.

    A Square
    A Square

    Rectangle

    A rectangle has 4 sides with parallel sides are of the same length and 4 corners are right angles. The rectangle has a longer side and a shorter side.

    A Rectangle
    A Rectangle

    Rhombus

    A rhombus is like a square or diamond with 4 equal sides, but none of the corners is a right angle.

    A Rhombus
    A Rhombus

    Parallelogram

    Any quadrilateral that has a pair of parallel sides is a parallelogram. There are shapes which demonstrate properties of parallelogram – square, rectangle, and a rhombus are special case of a parallelogram.

    A Paralleogram
    A Parallelogram

    Trapezoid

    If a quadrilateral contains at least one pair of parallel lines then it is called a trapezoid.

    A Trapezoid
    A Trapezoid

    Kite

    A kite is a quadrilateral that has a pair of adjacent sides of equal length and the angle where these two pair meet are equal.

    A Kite Diagram
    A Kite Diagram

    Flowchart

    Flowchart-Identifying Quadilateral
    Flowchart-Identifying Quadrilateral

    Program Code

    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
            float a, b, c, d, sum = 0.0;
            float angle[3], arr[3], len, tmp; int i;
            int flag, square, rectangle, rhombus, para, trap;
            flag = square = rectangle = rhombus = trap = 0;
            para = 0;
            printf("Enter length of all sides of the Quadrilateral:");
            scanf("%f", & arr[0]);
            scanf("%f", & arr[1]);
            scanf("%f", & arr[2]);
            scanf("%f", & arr[3]);
            len = arr[0];
            printf("Enter Angles of all Corners of the Quadrilateral:");
            for (i = 0; i < 4; i++)
           {
                    scanf("%f", & angle[i]);
            if (angle[i] == 90)
           {
                   flag++;
           }
    }
    
    /* 360 degree check */
            for (i = 0; i < 4; i++)
            {
                    sum = sum + angle[i];
            }
                    if (sum == 360)
                    {
                            printf("sum of angle is 360 n");
                    }
                    else
                    {
                            printf("Sum of Angles is not 360:n");
                            abort();
                    }
    
    /* Identify Square */
              for (i = 0; i < 4; i++)
             {
                      if (arr[i] == len)
                     {
                               square++;
    
                     if (square == 4 && flag == 4)
                    {
                              for (i = 0; i < 30; i++)
                              printf("_"); printf("\n\n");
                              printf("It is a Square:\n\n");
                              for (i = 0; i < 30; i++)
                              printf("_"); printf("\n\n");
                    }
             }
    }
    
    /* Identify rectangle */
    if (arr[0] == arr[2] && arr[1] == arr[3] && arr[0] != arr[3] && flag == 4)
    {
                    for (i = 0; i < 30; i++)
                    printf("_"); printf("\n\n");
                    printf("It is a Rectangle:\n\n");
                    for (i = 0; i < 30; i++)
                    printf("_"); printf("\n\n");
    }
    
    /* identify parallelogram */
              for (i = 0; i < 4; i++)
             {
                     if (angle[i] == 90)
                     {
                             para++;
                     }
             }
                     if (arr[0] == arr[2] || arr[1] == arr[3])
                    {
                         if (para < 4)
                        {
                             for (i = 0; i < 30; i++)
                             printf("_"); printf("\n\n");
                             printf("It is a Parallelogram :\n\n");
                             for (i = 0; i < 30; i++)
                             printf("_"); printf("\n\n");
                        }
                    }
    
    /* Identify Kites */
    if (arr[0] == arr[1] || arr[2] == arr[3])
    {
           if (para < 3 && angle[0] == angle[2] && angle[1] == angle[4])
           {
                    if (arr[0] != arr[2])
                     for (i = 0; i < 30; i++)
                     printf("_"); printf("\n\n");
                     printf("It is a Kite :\n\n");
                     for (i = 0; i < 30; i++)
                     printf("_"); printf("\n\n");
           }
    }
    
    /* Identify rhombus */
    for (i = 0; i < 4; i++)
    {
             if (arr[i] == len)
            {
                    rhombus++;
                    if (rhombus == 4 && angle[i] != 90)
                    {
                             for (i = 0; i < 30; i++)
                             printf("_"); printf("\n\n");
                             printf("It is a Rhombus:\n\n");
                             for (i = 0; i < 30; i++)
                             printf("_"); printf("\n\n");
                    }
             }
    }
    
    /* Identify trapezoid */
    for (i = 0; i < 4; i++)
    {
            len = arr[i];
            tmp = angle[i];
            for (i = 0; i < 4; i++)
           {
                    if (arr[i + 1] != len && angle[i] != tmp)
                    {
                            trap++;
                   }
            }
    }
    
    if (trap == 3)
    {
                    for (i = 0; i < 30; i++)
                    printf("_"); printf("\n\n");
                    printf("It is a Trapezoid!\n");
                    for (i = 0; i < 30; i++)
                    printf("_"); printf("\n\n");
    }
    system("PAUSE");
    return 0;
    }

    Output

    Enter length of all sides of the Quadrilateral:20
    20
    20
    20
    Enter Angles of all Corners of the Quadrilateral:90
    90
    90
    90
    sum of angle is 360
    ________________________________
    It is a Square:
    ________________________________
    Enter length of all sides of the Quadrilateral:12
    12
    12
    12
    Enter Angles of all Corners of the Quadrilateral:60
    120
    60
    120
    sum of angle is 360
    ____________________________
    It is a Parallelogram :
    ____________________________
    ____________________________
    It is a Rhombus:
    ____________________________