Program Abstraction:This article contains a simple C program to find the area of rectangle, circle and triangle.
Finding the area of rectangle, circle, or triangle is one of common questions in C practical examination. Finding the area of circle, rectangle and triangle may in a same question or question may ask you find area of anyone of above shape.
This is a C program to find area of a rectangle, circle and triangle in a same program. You can separate this program as your needs.
ØManually we can find the area of a rectangle by using equation: Area = Breadth * Length.
ØAlso we can find the area of a circle by using equation:
Area = 3.14 * (radius)2
ØAlso we can find the area of a triangle by using equation:
IF:
a = length of first side
b = length of second side
c = length of third side
s= (a + b+ c)/2
Area = sqrt(s*(s-a)*(s-b)*(s-c))
-where sqrt stands for square root
Program Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
void main()
{
int a,b,c,r, rectarea;
float circlearea, s1,s2,s3;
float s,triarea2;
clrscr();
printf("n Three Choices");
printf("n…………………….n 1- rectanglen 2- circlen 3- triangle");
printf("n Enter your choice :");
scanf("%d",&c);
switch(c)
{
case 1:
printf("n Enter breadth and length of rectangle:");
scanf("%d%d",&a,&b);
rectarea = a * b;
printf("Area of rectangle with breadth %d and length %d is %d units",a,b, rectarea);
break;
case 2:
printf("n Enter the radius of circle:");
scanf("%d",&r);
circlearea = 3.14 * r * r;
printf("Area of triangle with radius %d is %f units ",r, circlearea);
break;
case 3:
printf("n Enter the three side of triangle:n");
scanf("%f%f%f",&s1,&s2,&s3);
s = (s1 + s2 + s3)/2;
triarea2 =sqrt(s *((s - s1) * (s - s2) * (s - s3)));
printf("Area of triangle is %f",triarea2);
break;
default:
printf("Invalid choice");
}
getch();
}
Sample output for finding area of a rectangle with breadth and length 5 and 10 respectively:
Enter the breadth and length of rectangle:
5
10
Area of rectangle breadth 5 and length 10 is 50 units
Sample output for finding area of a triangle with the length of sides as 4, 5, and 6
Enter the length of the 3 sides of triangle:
4
5
6
Area of triangle is 9.921
Sample output for finding area of a circle with radius 3:
Enter the radius of circle : 3
Area of triangle with radius 3 is 28.260000 units
Be First to Comment