Area of a Triangle in C#
Problem Statement
We need to create a C# program that calculates the area of a triangle. The formula to calculate the area of a triangle given its base and height is:
Area=1/2×Base×Height
The program will prompt the user to enter the base and height of the triangle, then compute and display the area.
Logic and Approach
- Understanding the Formula: The area of a triangle can be calculated using the formula involving the base and height. This is a straightforward multiplication followed by division.
- User Input: The program should accept input for the base and height from the user.
- Calculation: Using the formula ( \text{Area} = \frac{1}{2} \times \text{Base} \times \text{Height} ), the program calculates the area.
- Output: The result is then displayed to the user.
C# Program: Area of a Triangle
C#
using System;
namespace TriangleAreaCalculator
{
class Program
{
static void Main(string[] args)
{
// Introduction message
Console.WriteLine("Welcome to the Triangle Area Calculator!");
// Getting the base input from the user
Console.Write("Enter the base of the triangle (in units): ");
double baseOfTriangle = Convert.ToDouble(Console.ReadLine());
// Getting the height input from the user
Console.Write("Enter the height of the triangle (in units): ");
double heightOfTriangle = Convert.ToDouble(Console.ReadLine());
// Calculating the area
double area = CalculateArea(baseOfTriangle, heightOfTriangle);
// Displaying the result
Console.WriteLine($"The area of the triangle is {area} square units.");
}
// Method to calculate the area of the triangle based on base and height
static double CalculateArea(double baseOfTriangle, double heightOfTriangle)
{
return 0.5 * baseOfTriangle * heightOfTriangle;
}
}
}
Code Explanation
1. Namespace and Class Declaration
C#
namespace TriangleAreaCalculator
{
class Program
{
namespace TriangleAreaCalculator
: The namespace groups related classes together and helps in avoiding name conflicts. Here, it encapsulates theTriangleAreaCalculator
program.class Program
: This is the main class of our application, containing the program logic.
2. Main Method
C#
static void Main(string[] args)
{
// Introduction message
Console.WriteLine("Welcome to the Triangle Area Calculator!");
static void Main(string[] args)
: The entry point of the application.static
means the method belongs to the class itself, andvoid
indicates that it does not return any value.Console.WriteLine("Welcome to the Triangle Area Calculator!");
: Displays a welcome message to the user.
3. User Input for Base
C#
Console.Write("Enter the base of the triangle (in units): ");
double baseOfTriangle = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the base of the triangle (in units): ");
: Prompts the user to input the base of the triangle.double baseOfTriangle = Convert.ToDouble(Console.ReadLine());
: Reads the user’s input as a string, converts it to a double type, and assigns it to thebaseOfTriangle
variable.
4. User Input for Height
C#
Console.Write("Enter the height of the triangle (in units): ");
double heightOfTriangle = Convert.ToDouble(Console.ReadLine());
- Similar to the base input, this code prompts the user for the height and converts the input to a double type.
5. Area Calculation
C#
double area = CalculateArea(baseOfTriangle, heightOfTriangle);
double area = CalculateArea(baseOfTriangle, heightOfTriangle);
: Calls theCalculateArea
method, passing the base and height as arguments, and assigns the returned value (area) to thearea
variable.
6. CalculateArea Method
C#
static double CalculateArea(double baseOfTriangle, double heightOfTriangle)
{
return 0.5 * baseOfTriangle * heightOfTriangle;
}
static double CalculateArea(double baseOfTriangle, double heightOfTriangle)
: This static method calculates the area of the triangle based on the given base and height. It takes two double arguments and returns a double.return 0.5 * baseOfTriangle * heightOfTriangle;
: This line calculates the area by multiplying 0.5 with the base and height and returns the result.
7. Display the Result
C#
Console.WriteLine($"The area of the triangle is {area} square units.");
Console.WriteLine(...)
: This line displays the calculated area to the user.
Conclusion
This C# program provides a simple way to calculate the area of a triangle using the base and height provided by the user. The program demonstrates basic concepts like reading user input, type conversion, and method creation in C#.