C Program To Print Area Of Triangle, Square, Circle, Rectangle And ...
Maybe your like
Problem
Write a program to calculate the area of triangle, square, circle, rectangle and polygon by using the switch case.
Solution
Based on case number, the area of triangle, square, circle, rectangle and polygon is calculated.
- The logic used to find area of triangle is as follows ?
Enter sides of a triangle a,b,c
s=(float)(a+b+c)/2; area=(float)(sqrt(s*(s-a)*(s-b)*(s-c)));- The logic used to find area of square is as follows ?
Enter the side of square at runtime.
area=(float)side*side;- The logic used to find area of circle is as follows ?
Enter the radius of circle at runtime
area=(float)3.14159*radius*radius;- The logic used to find area of rectangle is as follows ?
Enter length and breadth of rectangle at runtime
area=(float)len*breadth;- The logic used to find area of parallelogram is as follows ?
Enter base and height of parallelogram
area=(float)base*height;Example
Following is the C program to calculate the area of triangle, square, circle, rectangle and polygon by using the switch case ?
You can see the live demo here: Live Demo
#include<stdio.h> #include<math.h> int main() { int choice; printf("Enter1 to find area of Triangle2 for finding area of Square3 for finding area of Circle4 for finding area of Rectangle5 for Parallelogram"); scanf("%d", & choice); switch (choice) { case 1: { int a, b, c; float s, area; printf("Enter sides of triangle"); scanf("%d%d %d", & a, & b, & c); s = (float)(a + b + c) / 2; area = (float)(sqrt(s * (s - a) * (s - b) * (s - c))); printf("Area of Triangle is %f", area); break; } case 2: { float side, area; printf("Enter Sides of Square"); scanf("%f", & side); area = (float) side * side; printf("Area of Square is %f", area); break; } case 3: { float radius, area; printf("Enter Radius of Circle"); scanf("%f", & radius); area = (float) 3.14159 * radius * radius; printf("Area of Circle %f", area); break; } case 4: { float len, breadth, area; printf("Enter Length and Breadth of Rectangle"); scanf("%f %f", & len, & breadth); area = (float) len * breadth; printf("Area of Rectangle is %f", area); break; } case 5: { float base, height, area; printf("Enter base and height of Parallelogram"); scanf("%f %f", & base, & height); area = (float) base * height; printf("Enter area of Parallelogram is %f", area); break; } default: { printf("Invalid Choice"); break; } } return 0; }Output
When the above program is executed, it produces the following output ?
When the above program is executed, it produces the following output: Run 1: 1 to find area of Triangle 2 for finding area of Square 3 for finding area of Circle 4 for finding area of Rectangle 5 for Parallelogram 5 Enter base and height of Parallelogram 2 4 6 8 Enter area of Parallelogram is 8.000000 Run 2: 1 to find area of Triangle 2 for finding area of Square 3 for finding area of Circle 4 for finding area of Rectangle 5 for Parallelogram 3 Enter Radius of Circle 4.5 Area of Circle is 63.617199Also learn these topics to enhance your decision-making knowledge in C:
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
Tag » Area Of Triangle Rectangle
-
Area Formulas
-
Area Of Plane Shapes - Math Is Fun
-
Triangles: Area - Varsity Tutors
-
Area Of A Rectangle, Triangle, Circle & Sector, Trapezoid ... - YouTube
-
Area Of Triangles & Rectangles | Formula, Calculation & Examples
-
Area Of Triangles (article) | Geometry - Khan Academy
-
Area Of A Triangle: Relation To Rectangles - PBS LearningMedia
-
How To Find The Area Of A Triangle: Formula And Examples
-
How Do You Work Out The Area Of A Triangle? - BBC Bitesize
-
Area Of A Triangle - GeoGebra
-
[PDF] SQUARE Rectangle Triangle Trapezoid Circle
-
Area Of Composite Shapes Involving Triangles | CK-12 Foundation
-
How To Relate Areas Of Circle, Square, Rectangle And Triangle If They ...