This article presents a simple C# program to calculate the area of a rectangle based on user input. It illustrates how to use basic arithmetic operations and user input handling, making it a great example for beginners learning programming.
Code Example
Below is the complete C# code for calculating the area of a rectangle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | using System; class Program { static void Main(string[] args) { int area, length, width; Console.Write("Please write the length of your rectangle: "); length = Convert.ToInt32(Console.ReadLine()); Console.Write("Please write the width of your rectangle: "); width = Convert.ToInt32(Console.ReadLine()); area = length * width; Console.WriteLine("The area of the rectangle: {0}", area); Console.ReadKey(); } } |
Explanation of the Code
- User Input:
- The program prompts the user to enter the length and width of the rectangle
1 2 3 4 | Console.Write("Please write the length of your rectangle: "); length = Convert.ToInt32(Console.ReadLine()); |
Console.ReadLine()
takes the input as a string, andConvert.ToInt32()
converts it into an integer.
Calculation:
- The formula for the area of a rectangle is: Area=Length×Width
- This is implemented in the code as
1 2 3 | area = length * width; |
- The calculated area is displayed using
1 2 3 | Console.WriteLine("The area of the rectangle: {0}", area); |
- The
{0}
is a placeholder for the value of thearea
variable, which is passed after the string.
Pause Program:
- The
Console.ReadKey()
at the end keeps the console window open until a key is pressed, allowing the user to view the result.
Output:

Key Concepts
- Arithmetic Operations:
- The program demonstrates the use of the multiplication operator (
*
) to calculate the area.
- The program demonstrates the use of the multiplication operator (
- Input Conversion:
- The
Convert.ToInt32()
method ensures the user input is converted to an integer type for mathematical calculations.
- The
- Formatted Output:
- The
Console.WriteLine()
method with placeholders provides a clean and readable output.
- The
Enhancements
- Input Validation:
- To make the program more robust, you can validate the input to ensure the user enters valid integers:
1 2 3 4 5 6 | if (!int.TryParse(Console.ReadLine(), out length) || length <= 0) { Console.WriteLine("Invalid input. Please enter a positive integer for the length."); } |
Floating-Point Support:
- Modify the program to accept floating-point numbers for more precise calculations
1 2 3 4 5 6 7 8 9 10 11 12 13 | double length, width, area; Console.Write("Please write the length of your rectangle: "); length = Convert.ToDouble(Console.ReadLine()); Console.Write("Please write the width of your rectangle: "); width = Convert.ToDouble(Console.ReadLine()); area = length * width; Console.WriteLine("The area of the rectangle: {0}", area); |
- Perimeter Calculation:
- You can extend the program to calculate and display the perimeter of the rectangle as well: Perimeter=2×(Length+Width)
- User-Friendly Interface:
- Add labels to clearly separate input prompts and results for better readability.
Conclusion
This simple program demonstrates how to calculate the area of a rectangle in C# using basic arithmetic and user input. It serves as an excellent starting point for understanding input handling and basic mathematical operations in programming.