Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Tuesday, July 7, 2020

C program to find area of a triangle if base and height are given

Program to find area of a triangle if base and height are given:
By Ahmad Irshad | Programming Exercises and Solutions > find area of a triangle in C

Write a C program to find area of a triangle if base and height are given. Let's have a look at the below logic with step by step.

Area of a triangle if base & height are given:
Area of triangle | Protec
Where b represent base and h represent height of a triangle.


Required knowledge for this exercise is: 

Before writing any program you just imagine about output screen first. For example, what input to be given, and output to be displayed. Let's have a look at the below example: 

INPUT:

Enter three sides of a triangle = 15, 20

OUTPUT:

Area of a triangle = 150.00 sq. units


Logic to find area of a triangle if base & height are given:

Step by step logic to find area of a triangle.

1) Print a massage to the user like "Enter base & height of a triangle:" using printf();
2) Input base & height of a triangle from user and store it in variables base and height using scanf();
3) Now find area of a triangle using farmula area = (base * height) / 2;
4) At last print the value of area. that's it!


Program to find area of a triangle if all base & height are given: 

  1. /* C program to find area of a triangle if base & height are given */ #include <stdio.h>
  2. int main() { float area, base, height; /* Input base & height of a triangle from user */ printf("Enter base and height of a triangle: "); scanf("%f%f", &base, &height); /* To find area of a triangle use if base & height are given */
  3. area = (base * height) / 2;

  4. /* Print value of area */
  5. printf("Area of a triangle = %.2f sq. units\n", area);
  6. return 0; }

%.2f is used to print real values only up to 2 decimal places. You can also use %f to print up to 6 decimal places by default.


Output:

  1. Enter three sides of a triangle = 15, 20
  2. Area of a triangle = 150.00 sq. units





Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us