How to Create Pyramid in C#?

Geometrically speaking, a pyramid is a shape with a triangle base and a conical or triangular structure ascending to a point at the top. The triangular or polygonal base and a converging point are common aspects of pyramids, while the name "pyramid" is used to refer to a variety of shapes and constructions in different situations.

What is a Pyramid?

A pyramid is frequently a series of rows in programming or pattern-making where each row includes a certain arrangement of characters, like asterisks or numbers, which creates a shape that mimics a real geometric pyramid. This style is well-liked for teaching programming control structures like loops.

Here's how you use asterisks to make a basic pyramid pattern in C#.

using System;

class Mukesh
{
    static void Main()
    {
        int n, space, symbol;

        Console.Write("Enter the number of rows for the pyramid: ");
        n = int.Parse(Console.ReadLine());

        for (int i = 1; i <= n; i++)
        {
            // Print spaces
            for (space = 1; space <= n - i; space++)
            {
                Console.Write(" ");
            }

            // Print symbols (asterisks)
            for (symbol = 1; symbol <= 2 * i - 1; symbol++)
            {
                Console.Write("*");
            }

            // Move to the next line
            Console.WriteLine();
        }

        Console.ReadLine(); // Pause to view the pattern
    }
}

Let me explain each line in detail.

  • using System: This line includes the System namespace, which keeps essential classes for input and output operations.
  • class Mukesh: Defines a C# class named "Mukesh" All C# programs start with a class definition.
  • static void Main(): This is the entry point of the program. The Main method is the first to run when the program is run.
  • int n, space, symbol: Declares integer variables n, space, and symbol to be used later in the program.
  • Console.Write("Enter the number of rows for the pyramid: "): This line displays a message to the console, prompting the user to enter the number of rows for the pyramid.
  • n = int.Parse(Console.ReadLine());: Reads the user's input from the console and converts it to an integer. This value will determine the number of rows in the pyramid.
  • for (int i = 1; i <= n; i++): This for loop is used to iterate through each row of the pyramid, starting from the first row and going up to the specified number of rows (n).
  • for (space = 1; space <= n - i; space++): This is a nested loop which is used to print spaces before the symbols (asterisks). It calculates the number of spaces based on the row number (i) and the total number of rows (n).
  • for (symbol = 1; symbol <= 2 * i - 1; symbol++): Another nested for loop that is responsible for printing the symbols (asterisks). It calculates the number of symbols to be printed based on the row number (i).
  • Console.WriteLine: Moves to the next line, creating a new row for the pyramid.
  • Console.ReadLine(): This line is added to pause the program, allowing you to view the pattern on the console before it closes.

Output for n = 5;

Output

You can drop your queries related to this, if any.

Thanks :)


Recommended Free Ebook
Similar Articles