/***********************************************************
area.ch
Area
This program calculates the areas of
various shapes after asking the user
for the type of shape as well as
parameters of each shape.
************************************************************
Copyright 2014 SoftIntegration Inc.
http://www.softintegration.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
***********************************************************/
//Initialize variables
int choice;
double area, side, side1, side2, base, height, a, b, r;
while(1)//Infinite loop until break is used
{
//Ask for type of shape
printf("\nFor which type of polygon would you like to compute the area? \n");
printf("1) Square\n");
printf("2) Rectangle\n");
printf("3) Parallelogram\n");
printf("4) Trapezoid\n");
printf("5) Triangle\n");
printf("6) Circle\n");
printf("7) all done\n");
printf("Your Choice: ");
scanf("%d", &choice);
//Calculate and display areas based on case
if (choice > 7)//case of choice not being valid
printf("choose 1-7\n");
else if (choice == 1)//case of square
{
printf("what is the length of one side? ");
scanf("%lf", &side);
area = side * side;
printf("Area of square with side %.2lf = %.2lf", side, area);
}
else if (choice == 2)//case of rectangle
{
printf("what is the length of one side? ");
scanf("%lf", &side1);
printf("what is the length of the other side? ");
scanf("%lf", &side2);
area = side1 * side2;
printf("Area of rectangle = %.2lf", area);
}
else if (choice == 3)//case of parallelogram
{
printf("what is the length of the base? ");
scanf("%lf", &base);
printf("what is the height? ");
scanf("%lf", &height);
area = base * height;
printf("Area of parallelogram = %.2lf", area);
}
else if (choice == 4)//case of trapezoid
{
printf("what is the length of one base? ");
scanf("%lf", &a);
printf("what is the length of one base? ");
scanf("%lf", &b);
printf("what is the height? ");
scanf("%lf", &height);
area = .5 * height * (a+b);
printf("Area of trapezoid = %.2lf", area);
}
else if (choice == 5)//case of triangle
{
printf("what is the length of the base? ");
scanf("%lf", &base);
printf("what is the height? ");
scanf("%lf", &height);
area = .5 * base * height;
printf("Area of triangle = %.2lf", area);
}
else if (choice == 6)//case of circle
{
printf("what is the length of the radius? ");
scanf("%lf", &r);
area = 3.14159 * r * r;
printf("Area of circle = %.2lf", area);
}
else if (choice == 7)//case of finished
{
printf("bye!\n");
break;
}
}