C program to find perimeter, semi perimeter, and area of a triangle
Program to find perimeter, semi perimeter, and area of a triangle:
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:
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:
-
#include <stdio.h>
#include <math.h>
-
int main()
{
double a, b, c, perimeter, s, area;
printf("\nEnter three sides of a triangle: ");
scanf("%lf%lf%lf", &a, &b, &c);
-
perimeter = a + b + c;
- s = (a + b + c)/2;
- area = sqrt(s*(s-a)*(s-b)*(s-c));
-
printf("Perimeter of triangle = %.2lf units\n", perimeter);
printf("Semi-perimeter of triangle = %.2lf units\n", s);
- printf("Area of a triangle = %.2lf sq. units\n", area);
-
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:
- Enter three sides of a triangle = 5, 6, 7
- Perimeter of a triangle = 18.00 units
- Semi-perimeter of a triangle = 9.00 units
- Area of a triangle = 14.70 sq. units
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.