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 perimeter, semi perimeter, and area of a triangle

Program to find perimeter, semi perimeter, and area of a triangle:
By Ahmad Irshad | Programming Exercises and Solutions > find perimeter, semi-perimeter, & area of a triangle in C

Write a C program to find perimeter, semi-perimeter, and area of a triangle if all sides are given. Let's have a look at the below logic.

Area of a triangle if all sides are given:

Heron's Formula | Protec

Farmula to be used:
  1. perimeter = a + b + c
  2. s = (a + b + c) / 2 // where s represent semi-perimeter
  3. area = sqrt(s*(s-a)*(s-b)*(s-c)) // Where a, b, and c are the sides 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 = 5, 6, 7

OUTPUT:

Perimeter of a triangle = 18.00 units
Semi-perimeter of a triangle = 9.00 units
Area of a triangle = 14.70 sq. units


Logic to find perimeter, semi-perimeter, & area of a triangle:

Step by step logic to find any angle of a triangle.

1) Print a massage to the user like "Enter all sides of a triangle:" using printf();
2) Input three sides of a triangle from user and store it in variables a, b, and c using scanf();
3) Now find perimeter, semi-perimeter & area of a triangle using below farmula
perimeter = a + b + c;  s = (a + b + c) / 2; and area = sqrt(s*(s-a)*(s-b)*(s-c));
4) At last print the value of perimeter, s, & area. that's it!


Program to find perimeter, semi-perimeter and area of a triangle if all sides are given: 

  1. /* C program to find perimeter, semi-perimeter, & area of a triangle */ #include <stdio.h> #include <math.h>
  2. int main() { double a, b, c, perimeter, s, area; /* Input three sides of a triangle from user */ printf("\nEnter three sides of a triangle: "); scanf("%lf%lf%lf", &a, &b, &c); /* You can find perimeter, semi-perimeter, & area of a triangle.
  3. * by using follwing farmula:
  4. * perimeter = a + b + c | semi-perimeter = (a + b + c) / 2
  5. * area = sqrt(s*(s-a)*(s-b)*(s-c)) | where s is semi-perimeter.
  6. */ perimeter = a + b + c;
  7. s = (a + b + c)/2;
  8. area = sqrt(s*(s-a)*(s-b)*(s-c));
  9. /* Print values of perimeter, s, and area */ printf("Perimeter of triangle = %.2lf units\n", perimeter); printf("Semi-perimeter of triangle = %.2lf units\n", s);
  10. printf("Area of a triangle = %.2lf sq. units\n", area);
  11. return 0; }

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

Output:

  1. Enter three sides of a triangle = 5, 6, 7
  2. Perimeter of a triangle  = 18.00 units
  3. Semi-perimeter of a triangle = 9.00 units
  4. Area of a triangle = 14.70 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