C# program to calculate the perimeter of Rectangle

Here, we are going to learn how to calculate the perimeter of Rectangle in C#? By Nidhi Last updated : April 15, 2023

Perimeter of Rectangle Formula

Here, we will calculate the perimeter of the Rectangle using the below formula.

perimeter = 2 * (length + width);

C# program to calculate the perimeter of Rectangle

The source code to calculate the perimeter of the Rectangle is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//Program to calculate the perimeter of the 
//rectangle in C#

using System;

class Rectangle
{
    public static int Main()
    {
        float length    = 0.0F;
        float width   = 0.0F;
        float perimeter = 0.0F;

        Console.Write("Enter the value of length: ");
        length = float.Parse(Console.ReadLine());

        Console.Write("Enter the value of width: ");
        width = float.Parse(Console.ReadLine());

        perimeter = 2 * (length + width);

        Console.WriteLine("Perimeter of rectangle: "+perimeter);
        return 0;
    }
}

Output

Enter the value of length: 5
Enter the value of width: 7
Perimeter of rectangle: 24
Press any key to continue . . .

Explanation

Here, we created a class Rectangle that contains a method Main(). The Main() method is the entry point for the program. Here we read the value of length and width and calculate the perimeter of the Rectangle using the below formula, and then print the result on the console screen.

perimeter = 2 * (length + width);

C# Basic Programs »


Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.