C program to find area and perimeter of equilateral triangle
C Program to find area and perimeter of equilateral triangle:
Write a C program to find area and perimeter of equilateral triangle if it's sides are given.
Before writing any program you just imagine about output screen first. Like what input to be given, and output to be displayed. Let's have a look at the below example.
- INPUT
- Enter side of an equilateral triangle = 15
OUTPUT
Enter side of an equilateral triangle = 97.43 sq.units
Perimeter of an equilateral triangle = 43.3 units
Required knowledge for this exercise is:
Area & perimeter of equilateral triangle if it's sides are given:
Use farmula to find perimeter of an equilateral triangle, P = 3 * Side
Where S = Side, A = Area, and P = Peremeter.
Logic to find area and perimeter of equilateral triangle if sides are given:
Step by step logic to find area and perimetre of equilateral triangle.
1) Print a massage to the user like "Enter side of an equilateral triangle:" using printf();
2) Input side of an equilateral triangle from user and store it in a variable side using scanf();
3) Now find area and perimeter of equilateral triangle using below farmula
area = sqrt(3) / 4 (side * side); and perimeter = 3 * side;
4) At last print the value of area and perimeter. that's it!
Program to find area & perimeter of an equilateral triangle if sides are given:
-
#include <stdio.h>
#include <math.h>
int main()
{
float side, area, perimeter;
printf("Enter side of an equilateral triangle: ");
scanf("%f", &side);
area = (sqrt(3) / 4) * (side * side);
perimeter = 3 * side
-
printf("Area of equilateral triangle = %.2f sq. units", area);
printf("Perimeter of equilateral triangle = %.2f units", perimeter);
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:
- Enter side of an equilateral triangle = 15
- Area of equilateral triangle = 97.43 sq. units
- Perimeter of equilateral triangle = 45 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.