Rectangle Perimeter Calculator in CSharp

Rectangle Perimeter Calculator in CSharp


Perimeter is the total length of the boundary of a two-dimensional shape. It is a measure of the distance around the outside of a shape. For a rectangle, the perimeter is calculated by adding the lengths of all its sides. In the case of a rectangle, the formula for calculating the perimeter is 2 times the sum of the length and width of the rectangle.

In this lesson, we delve into the fundamental concept of computing the perimeter of a rectangle using C#. The program allows us to understand the essential elements involved in calculating the perimeter of a geometric shape, emphasizing the significance of this process in various mathematical and practical contexts. By exploring the details of the code and its application, we aim to develop a comprehensive understanding of how to implement this geometric operation.


  • Understand the concept of perimeter in geometry and its significance in real-world scenarios. Apply:
  • Apply the knowledge of perimeter calculation to solve problems and perform geometric operations in practical applications. Create:
  • Develop a functional C# program to compute the perimeter of a rectangle, demonstrating proficiency in coding and algorithmic thinking. Upgrade:
  • Explore and implement advanced features or modifications to the program, showcasing the ability to enhance the program’s functionality and performance.

Source code example

Rectangle Perimeter Calculator in CSharp - source code
Rectangle Perimeter Calculator in CSharp – source code
using System;

namespace RectanglePerimeter
class Program
static void Main(string[] args)
Console.WriteLine("Rectangle Perimeter Calculator");

// Ask the user for the length of the rectangle
Console.Write("Enter the length of the rectangle: ");
double length = Convert.ToDouble(Console.ReadLine());

// Ask the user for the width of the rectangle
Console.Write("Enter the width of the rectangle: ");
double width = Convert.ToDouble(Console.ReadLine());

// Calculate the perimeter
double perimeter = 2 * (length + width);

// Display the result
Console.WriteLine($"Perimeter of the rectangle: {perimeter}");

Console.WriteLine("Press any key to exit.");


General explanation

This C# program calculates the perimeter of a rectangle based on user input for the length and width of the rectangle. It prompts the user to provide the length and width of the rectangle, converts the input to double data type, and then computes the perimeter using the formula: 2 * (length + width). The program then displays the computed perimeter, allowing the user to view the result. Finally, it prompts the user to press any key to exit the program.

Line by line explanation

  • The first line of the code is an import statement that allows the program to use classes and methods from the “System” namespace, which contains fundamental classes and base classes.
  • The “RectanglePerimeter” namespace groups related code together, helping to organize and manage the application.
  • The “Program” class serves as the entry point of the program, containing the Main method, which is where the program execution begins.
  • “Console.WriteLine” is used to print out the string “” as a header.
  • The next line prints “Rectangle Perimeter Calculator” as a header.
  • The comment “Ask the user for the length of the rectangle” is a descriptive note for the user’s understanding.
  • “Console.Write” prompts the user to enter the length of the rectangle, and “Convert.ToDouble(Console.ReadLine())” takes the user input and converts it into a double data type, storing it in the “length” variable.
  • Similarly, the program asks the user to input the width of the rectangle and stores the value in the “width” variable.
  • The “perimeter” is calculated using the formula 2 * (length + width) and is stored in the “perimeter” variable.
  • The Console.WriteLine statement displays the calculated perimeter to the user.
  • Finally, the program waits for any key press to exit.


Rectangle Perimeter Calculator in CSharp - output
Rectangle Perimeter Calculator in CSharp – output




This lesson introduced the creation of a C# program to calculate the perimeter of a rectangle. Key concepts included user input collection, data type conversion, and performing calculations based on the provided inputs. The program demonstrated the utilization of the formula 2 * (length + width) to compute the perimeter. By exploring this program, learners can gain a better understanding of handling user input and performing basic calculations in C#. Experimenting with variations of the code can deepen their comprehension of these fundamental concepts.

Exercises and Assessment

Here are some suggested exercises and challenges for the lesson:

  1. Advanced Challenges:
  1. Implement error handling for cases where negative values are input for length or width.
  2. Enhance the program to calculate the area of the rectangle in addition to the perimeter.
  3. Enable the program to handle decimal values for length and width, ensuring accurate calculations.
  1. Further Enhancements:
  1. Develop a graphical user interface for the rectangle perimeter calculator.
  2. Create a version of the program that can calculate the perimeters of various shapes, including squares and triangles.
  3. Enable the program to calculate the diagonal length of the rectangle in addition to the perimeter.

Related Topics and Articles:

Course Outline in C#

You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.

Hire our team to do the project.

, , , , , , , , , ,

Post navigation