area of the trapezium

Area of the Trapezium - C Programming Example

This C Program code will take the lengths of two sides and the height of a trapezium as input and will show the area as output.

C Programming example
Area of a Trapezium
A trapezium is a quadrilateral that has only one pair of parallel sides. 
Consider the area of the following trapezium.
C Programming Example
To calculate the area of a trapezium, divide it into a rectangle and two triangles as shown below.
C Programming example
C Programming Example
Two triangle and a rectangle

C Programming Code

    float a, b, h, area;
    printf("Input the length of one arm of the trapezium \n");
    scanf("%f", &a);
    printf("Input the length of the other arm of the trapezium \n");
    scanf("%f", &b);
    printf("Input the hight of the trapezium \n");
    scanf("%d", &h);
    area= 0.5*h* (a+b);
    printf("The area of the trapezium is= %d", area);


C Programming Example

Area of a Trapezium


·      #include<stdio.h>
Ø This are called header file.A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source filesThe C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header<stdio.h>.In the other hand<conio.h> is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C.This header declares several useful library functions for performing "console input and output" from a program.
·       main()
Ø In C, program execution starts from the main() function. The main function can in-turn call other functions. When main calls a function, it passes the execution control to that function. The function returns control to main when a return statement is executed or when end of function is reached.
·       float a,area;
Ø float is a variable or data type. A variable declared to be of type float can be used for storingfloating-point numbers (values containing decimal places).Here we declare “a”and  “area”variable in float type. So this two variable can take values containing decimal number.
·       printf() function
Ø C uses printf() function to write from the input devices. This function has been declared in the header file called stdio.h. Any text written within the pair of quotes ("") is displayed as such by printf() function on the screen. 
·       scanf() function
Ø This function is used to get input from the user of the program.scanf(“%f”,&a, &b, &h”)is used here. So, this program will read in a float value for “a”, “b”, “h” variable that the user enters on the keyboard(%f is for float values.
·       printf("The area of the trapezium is %f \n",area);
Ø %f is used to print the main result. As “area” variable is declared after the “ ”sign, this will print the value of “area” valuable.
·       getch() function
Ø getch() is used to hold the console(output) window on the screen after the whole program run is completed till the user enters a key from keyboard. This function is present in the header file called conio.h.

Final Result

This program will show you the area of a trapezium. What you need to do is, just input the length and the height the trapezium.

No comments

Powered by Blogger.