1. 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static void Main()
{
Console.Write("Enter length: ");
double length = double.Parse(Console.ReadLine());
Console.Write("Enter width: ");
double width = double.Parse(Console.ReadLine());
Rectangle rect = new Rectangle(length, width);
Console.WriteLine($"Length: {length}");
Console.WriteLine($"Width: {width}");
Console.WriteLine($"Length: {rect.GetArea()}");
}
}
struct Rectangle
{
private double length;
private double width;
public Rectangle(double length, double width)
{
this.length = length;
this.width = width;
}
public double GetArea()
{
return length * width;
}
}
Comments
Leave a comment