Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Rectangle Area: The program will ask the user to enter the length and width of a

ID: 3724716 • Letter: R

Question

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 computer screen. The program’s Main function must call the following four functions written in C#:

• 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 (see sample output below). Input Validation:

Do not accept length and width values lower than 0.

Explanation / Answer

C PROGRAMME :

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 :

$mcs *.cs -out:main.exe

$mono main.exe

Enter the length of rectangle:

Enter the width of rectangle:

Length: 2 Width: 3 Area: 6

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote