Chapter 5.

Write a program that will calculate the area of either a circle, rectangle or triangle. Prompt the user for the type of calculation to perform. Allow more than one calculation to be performed. Use the following formulae:

Area of Circle = PI * radius2

Area of Rectangle = Height * Width

Area of Triangle = (Height * Width) /2

 

using System;

namespace Shape
{

class Class1
{

[STAThread]
static void Main(string[] args)
{

int ans;
const double pi=3.14;
double circleArea;
double triangleArea;
double radius;
double triangleHeight;
double triangleWidth;
double rectangleArea;
double rectangleHeight;
double rectangleWidth;

do
{

Console.WriteLine("1.) Circle 2.)Triangle 3.)Rectangle 4.)Exit");
Console.WriteLine("Select a shape: ");
ans = Convert.ToInt32(Console.ReadLine());

switch (ans)
{

case 1:
Console.WriteLine("You have selected a circle");
Console.WriteLine("Enter the radius of the circle");
radius = Convert.ToDouble(Console.ReadLine());
circleArea = pi * radius;
Console.WriteLine("The area of the circle is {0}", circleArea);
break;

case 2:
Console.WriteLine("You have selected a triangle");
Console.WriteLine("Enter the height of the triangle");
triangleHeight = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter the width of the triangle");
triangleWidth = Convert.ToDouble(Console.ReadLine());
triangleArea = (triangleHeight * triangleWidth)/2;
Console.WriteLine("The area of the circle is {0}", triangleArea);
break;

case 3:
Console.WriteLine("You have selected a rectangle");
Console.WriteLine("Enter the height of the rectangle");
rectangleHeight = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter the width of the rectangle");
rectangleWidth = Convert.ToDouble(Console.ReadLine());
rectangleArea = (rectangleHeight * rectangleWidth)/2;
Console.WriteLine("The area of the circle is {0}", rectangleArea);
break;

default:
Console.WriteLine("Not a valid option");
break;

}

} while (ans != 4);

}

}

}