In C# Create a program to find the area of a rectangle. Rectangle Area: The prog
ID: 3597242 • Letter: I
Question
In C#
Create a program to find the area of a rectangle.
Rectangle Area: The program will ask the user to enter the length and width of a rectangle, and then display the rectangle’s length, width and area on the screen. The program cannot accept input values less than 0. The program’s Main function must call the following four function:
getLength – This function should ask the user to enter the rectangle’s length, validate it, and then return that value as a double to the main function.
getWidth - This function should ask the user to enter the rectangle’s width, validate it, and then return that value as a double to the main function.
getArea – This function should accept the rectangle’s length and width as arguments, compute the rectangle’s area with these arguments and return the rectangle’s area to the main function. The area is calculated by multiplying the length by the width.
displayData – This function should accept the rectangle’s length, width, and area as arguments, and display them in an appropriate message on the computer screen.
Thank you.
Explanation / Answer
using System.IO;
using System;
class Program
{
static void Main()
{
double l = getLength();
double w = getWidth();
double area = getArea(l, w);
displayData(l, w, area);
}
static double getLength() {
Console.WriteLine("Enter the length of rectangle: ");
double l = Double.Parse(Console.ReadLine());
while(l < 0) {
Console.WriteLine("Invalid Input. Enter the length of rectangle: ");
l = Double.Parse(Console.ReadLine());
}
return l;
}
static double getWidth() {
Console.WriteLine("Enter the width of rectangle: ");
double w = Double.Parse(Console.ReadLine());
while(w < 0) {
Console.WriteLine("Invalid Input. Enter the width of rectangle: ");
w = Double.Parse(Console.ReadLine());
}
return w;
}
static double getArea(double l, double w) {
return l * w;
}
static void displayData(double l, double w, double area) {
Console.WriteLine("Length: "+l+" Width: "+w+" Area: "+area);
}
}
Output: