This article introduces a simple C# program to calculate the area and perimeter of a square based on user input. This program is a great example for beginners, as it covers basic user input handling, arithmetic operations, and formatted output.
Code Example
Here is the full C# program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | using System; class Program { static void Main(string[] args) { int squareHeight, area, perimeter; Console.Write("What is the height of your square? : "); squareHeight = Convert.ToInt32(Console.ReadLine()); area = squareHeight * squareHeight; perimeter = 4 * squareHeight; Console.WriteLine("Area : {0}\nPerimeter : {1}", area, perimeter); // "\n" --> new line Console.ReadKey(); } } |
Output:
