0% found this document useful (0 votes)
21 views2 pages

Overloaded Area Calculator Assignment

This C# program uses a menu driven do-while loop to calculate the areas of different shapes. The user is prompted to select calculating the area of a square, rectangle, triangle or exit. Based on their selection, the appropriate area formula is called to calculate the area using side lengths read from user input. The results are output and the user is returned to the menu until exiting.

Uploaded by

addison.jayne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Overloaded Area Calculator Assignment

This C# program uses a menu driven do-while loop to calculate the areas of different shapes. The user is prompted to select calculating the area of a square, rectangle, triangle or exit. Based on their selection, the appropriate area formula is called to calculate the area using side lengths read from user input. The results are output and the user is returned to the menu until exiting.

Uploaded by

addison.jayne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System;

class AreaCalculator
{
static void Main()
{
int choice;

do
{
Console.WriteLine("What Would you like to calculate?:");
Console.WriteLine("1) Area of a Square");
Console.WriteLine("2) Area of a Rectangle");
Console.WriteLine("3) Area of a Triangle");
Console.WriteLine("4) Exit");
Console.Write("Type your choice (1-4): ");

if (int.TryParse(Console.ReadLine(), out choice))


{
switch (choice)
{
case 1:
Console.Write("Enter the side length of the square\nUnits:
");
double sideLength = Convert.ToDouble(Console.ReadLine());
Console.WriteLine($"Area of the square: {Area(sideLength)}\
n");
break;

case 2:
Console.Write("Enter the length of the rectangle\nUnits:
");
double length = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the width of the rectangle\nUnits: ");
double width = Convert.ToDouble(Console.ReadLine());
Console.WriteLine($"Area of the rectangle: {Area(length,
width)}\n");
break;

case 3:
Console.Write("Enter the length of side 1 of the triangle\
nUnits: ");
double side1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the length of side 2 of the triangle\
nUnits: ");
double side2 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the length of side 3 of the triangle\
nUnits: ");
double side3 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine($"Area of the triangle: {Area(side1,
side2, side3)}\n");
break;

case 4:
Console.WriteLine("Goodbye!");
break;

default:
Console.WriteLine("Please enter a number between 1 and
4.");
break;
}
}
else
{
Console.WriteLine("Please enter a valid number.");
}

Console.WriteLine();

} while (choice != 4);

static double Area(double side)


{
return side * side;
}

static double Area(double length, double width)


{
return length * width;
}

static double Area(double side1, double side2, double side3)


{
double s = (side1 + side2 + side3) / 2;

return Math.Sqrt(s * (s - side1) * (s - side2) * (s - side3));


}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy