Gerrit Botes - 1 year ago 48
C# Question

# Writing the formula for surface area of a rectangular pyramid in C#

I need help in writing the formula for the total surface area of a rectangular pyramid. This is the formula I want to write in C#:

This is what I have so far and I've tried various variations(like using Math.Pow etc.) of it but still no progress:

`````` resultPyramidArea = "Pyramid area: " + (length3d * width3d + length3d) * Math.Sqrt(((width3d / 2.000) * (width3d / 2.000)) + (height3d * height3d)) + width3d * Math.Sqrt(((length3d / 2.000) * (length3d * 2.000)) + (height3d * height3d));
``````

In the formula, A is Area, l is replaced with length3d, w with width3d and h with height3d

Any help would be much appreciated.

Your order of operations is incorrect. It should be this:

``````resultPyramidArea =
(length3d * width3d)
+ (length3d * Math.Sqrt(Math.Pow(width3d / 2, 2) + Math.Pow(height3d, 2)))
+ (width3d * Math.Sqrt(Math.Pow(length3d / 2, 2) + Math.Pow(height3d, 2)));
``````

Edit: Code I used to test this statement for you OP:

``````static void Main()
{
double length3d = 4;
double width3d = 2;
double height3d = 3;
double resultPyramidArea = (length3d * width3d) + (length3d * Math.Sqrt(Math.Pow(width3d / 2, 2) + Math.Pow(height3d, 2))) + (width3d * Math.Sqrt(Math.Pow(length3d / 2, 2) + Math.Pow(height3d, 2)));
Console.WriteLine(resultPyramidArea);
It's correct, I tried the inputs on google's online calculator and got the correct answer. If you are still having problems its probably because in your original code `resultPyramidArea` is adding a string and a double, This is not a valid statement in C# if resultPyramidArea is a double type variable , if you want to display the "Pyramid area: " message, do this instead:
``````Console.WriteLine("Pyramid area: {0}", resultPyramidArea)