In this program we are going to write a “C program to find the area of triangle”. But first we have to know about the triangle. What is a Triangle? Three sided Polygon is known as triangle. To find the area of triangle we must have a base and height of triangle.
Area of Triangle = ½(b × h) where b is base and h is height or You simply use Area of Triangle=0.5*(b*h) where b is the base of triangle and h is the height of the triangle. So let’s start creating the “C program to find the area of triangle”
C program to find the area of triangle:
First we have to take the variable for the base height and area of the triangle. In this program we are using base for “base” and height for “height“. First we have to enter the base of the triangle by using scan function(scanf) after taking the value of base now user is asked to enter the value of height.
After taking the value of base and height of triangle now we have to calculate the area of triangle. We know that area of triangle is equal to half of multiplication of base and height (base*height)/2. After entering the values of base and height compiler we have to write the area of triangle’s formula.
After operations are applied then the value of area of triangle assigns to their respective variable and then the area of triangle will be displayed on your system’s screen.
Program:
//C Program to Find the area of triangle// #include<stdio.h> int main(){ float base, height, area_of_triangle; //Enter the value of length of triangle from the user// printf("Enter the base of the Triangle:\n"); scanf("%f",&base); //Enter the value of height of triangle from the user// printf("Enter the height of the Triangle:\n"); scanf("%f",&height); //Now we are using formula of area of triangle// area_of_triangle= 0.5*(base*height); //Area of Triangle will be displayed on the screen// printf("Area Of Triangle=%f",area_of_triangle); return 0; }
so, this is the logic of whole program, in which we have given the formula of area of triangle, and in this program, we have used float data type because we want output in decimal form and we have also taken value from user in float data type, for that we have to use format specifier %f, which specifies the float data type. you can also use int or double data type.
We have taken two values from user, which are height and base according to the program, and for both these two values, we have used float data type, which will give us value in decimal but if we use int or double, then in this case, there will be no difference, but in some cases, it will effect your output.
Output:
In the above image it is clearly shown that user enters the value of base=4 and height=7 compiler will display the area of triangle on your screen.
Conclusion:
From the above code we concluded that how to write a C Program To Find The Area Of Triangle simply by using their formula. Now we are able to find the area of triangle. If you are searching for a C program to calculate the area of rectangle then click on the link to direct to the program page in our website.
[sc_fs_faq html=”true” headline=”h4″ img=”” question=”What is the formula for any triangle?” img_alt=”” css_class=””] The basic formula for the area of triangle is the product of base and height divided by 2. [/sc_fs_faq]
[sc_fs_faq html=”true” headline=”h4″ img=”” question=”Can we calculate the area of triangle other than 1/2*base*height formula?” img_alt=”” css_class=””] Yes, we can also use Area = √[s(s-a)(s-b)(s-c)] where s is the semi perimeter of the triangle i.e s = (a + b + c)/2. [/sc_fs_faq]