Answer to Question #332274 in C# for Jen

Question #332274

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

1
Expert's answer
2022-04-22T04:43:53-0400
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.");
            }
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS