settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: How to calculate an area of a rectangle in C# and Visual Basics

+7 votes

How to create a program in C# (and Visual Basics) which calculates the area of a rectangle?

asked in C# category by user ak47seo

3 Answers

+4 votes
 
Best answer

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);
    }
}

calculating the area of a rectangle in c sharp (C#) and Visual Basics

answered by user paulcabalit
edited by user golearnweb
+4 votes

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);
    }
}
answered by user andrew
edited by user golearnweb
+4 votes

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);

    }
}
answered by user nikole
edited by user golearnweb
...