4
07/11/2024 2:26 p.m.
Themenstarter
How to create a program in C# (and Visual Basics) which calculates the area of a rectangle?
3 Antworten
2
07/11/2024 2:27 p.m.
To find the area of a rectangle, multiply the length by the width.
The formula is:
A = L x W
where A is the area, L is the length, W is the width, and X means multiply
and the code is:
using System;
class AreaRectangle
{
static void Main()
{
//Rectangle Area: A (area) = L (length) x W (width)
Console.Write("Please write the length of your rectangle: ");
decimal lengthSide = decimal.Parse(Console.ReadLine());
Console.Write("Please write the width of your rectangle: ");
decimal widthSide = decimal.Parse(Console.ReadLine());
decimal area = lengthSide * widthSide;
Console.WriteLine("The area of your rectangle is: {0}", area);
}
}

2
07/11/2024 2:28 p.m.
and another one:
using System;
class Rectangles
{
static void Main()
{
//Problem 4. Write an expression that calculates rectangle’s perimeter and area by given width and height.
Console.Write("Enter the rectangle's height:");
double height = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the rectangle's width:");
double width = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("P = {0}", 2 * (width + height));
Console.WriteLine("A = {0}", width * height);
}
}
1
07/11/2024 2:27 p.m.
Here's another solution - including the calculation of the perimeter of the rectangle:
using System;
class Rectangles
{
static void Main()
//Area of a rectangle: A = width * height
//Perimeter of a rectangle: P = 2*(width + height)
{
Console.WriteLine("Please write the WIDTH of your rectangle:");
double width = double.Parse(Console.ReadLine());
Console.WriteLine("Please write the HEIGHT of your rectangle:");
double height = double.Parse(Console.ReadLine());
double area = width * height;
double perimeter = 2 * (width + height);
Console.WriteLine("The AREA of your rectangle is: {0}", area);
Console.WriteLine("The PERIMETER of your rectangle is: {0}", perimeter);
}
}
