In: Computer Science
Please code in C# - (C - Sharp)
Assignment Description
Write out a program that will ask the user for their name; the length and width of a rectangle; and the length of a square. The program will then output the input name; the area and perimeter of a rectangle with the dimensions they input; and the area and perimeter of a square with the length they input.
Tasks
Code Screenshots:
Sample Output:
Code To Copy:
//Use the
//required modules.
using System;
//Define
//the MainClass.
class MainClass
{
//Define the
//Main() method.
public static void Main(string[] args)
{
//Declare the
//required variables.
string name;
double rect_length;
double rect_width;
int square_length;
double area_rect;
int area_square;
double perimeter_rect;
int perimeter_square;
//Prompt the user
//to enter the name.
Console.Write("Enter the name: ");
//Read and store the name.
name = Console.ReadLine();
//Prompt the user to enter
//the length of the rectangle.
Console.Write("Enter the length of the rectangle: ");
//Read and store the
//length of the rectangle.
rect_length = Double.Parse(Console.ReadLine());
//Prompt the user to enter
//the width of the rectangle.
Console.Write("Enter the width of the rectangle: ");
//Read and store the
//width of the rectangle.
rect_width = Double.Parse(Console.ReadLine());
//Prompt the user to enter
//the length of the square.
Console.Write("Enter the length of the square: ");
//Read and store the
//length of the square.
square_length = Convert.ToInt32(Console.ReadLine());
//Calculate the area
//of the rectangle.
area_rect = rect_length * rect_width;
//Calculate the
//perimeter of the rectangle.
perimeter_rect = (2 * rect_length) + (2 * rect_width);
//Calculate the
//area of the square.
area_square = square_length * square_length;
//Calculate the
//perimeter of the square.
perimeter_square = 4 * square_length;
//Display the name.
Console.WriteLine("Name: {0}", name);
//Display the required results.
Console.WriteLine("Area of rectangle: {0}", area_rect);
Console.WriteLine("Perimeter of rectangle: {0}", perimeter_rect);
Console.WriteLine("Area of square: {0}", area_square);
Console.WriteLine("Perimeter of square: {0}", perimeter_square);
Console.WriteLine("Press enter to continue");
Console.Read();
}
}