Write a program to declare a structure and inside it is a non-static
method that returns the area of a rectangle.
Test Data and Sample Output:
Input the dimensions of a rectangle:
Length: 60
Width: 30
Expected Output:
Length: 60
Width: 30
Area: 1800
using System;
namespace ConsoleApp1
{
class Program
{
public class Rectangle
{
double l, w;
public Rectangle(double length, double width)
{
l = length;
w = width;
}
public double Area()
{
return l * w;
}
}
static void Main(string[] args)
{
try
{
Console.WriteLine("Input the dimensions of a rectangle:");
Console.Write("Length: ");
string s = Console.ReadLine();
double length = Convert.ToDouble(s);
Console.Write("Width: ");
s = Console.ReadLine();
double width = Convert.ToDouble(s);
Rectangle rect = new Rectangle(length, width);
Console.WriteLine("Area: {0}", rect.Area());
}
catch
{
Console.WriteLine("Bad number.");
}
}
}
}
Comments
Leave a comment