find the surface area of a pyramid in csharp

To find the surface area of a pyramid in C#, we need to use its formula which is given as:

Surface Area = (1/2) x Perimeter of Base x Slant Height + Base Area

Here's an implementation in C#:

main.cs
using System;

class Pyramid
{
    static void Main(string[] args)
    {
        double length, width, height, slantHeight, baseArea, surfaceArea;

        Console.WriteLine("Enter length of base: ");
        length = double.Parse(Console.ReadLine());

        Console.WriteLine("Enter width of base: ");
        width = double.Parse(Console.ReadLine());

        Console.WriteLine("Enter height of pyramid: ");
        height = double.Parse(Console.ReadLine());

        // Calculate the slant height
        slantHeight = Math.Sqrt(Math.Pow(length/2, 2) + Math.Pow(height, 2));

        // Calculate the base area
        baseArea = length * width;

        // Calculate the surface area
        surfaceArea = (0.5 * (length + width) * slantHeight) + baseArea;

        Console.WriteLine("Surface Area of Pyramid: {0}", surfaceArea);
    }
}
844 chars
30 lines

In this code, we ask the user to input the length, width, and height of the pyramid, and then we calculate the slant height using the formula. We also calculate the base area by multiplying the length and width together. Finally, we plug these values into the surface area formula to find the surface area of the pyramid.

gistlibby LogSnag