C# Program to Print Pyramid From Console Application

Here i am posting a simple method for printing a Pyramid type output given by the rows values.

i.e Users has to give the rows values that means if some users give value 5 then the length of the pyramid will be of five.


As given in the output i have given the row value 10.

In this case we have 3 loops to be performed :

1) for let knowing which row is to b printed.

2) for printing the spaces between each stars "*"

3) for printing the stars in output "*"

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PostslushDemo
{
    class Program
    {
        public static void print(int no)
        {

            for (int i = 1; i <= no; i++)
            {

                for (int j = i; j <= no; j++)
                {
                    Console.Write("  ");

                }

                for (int k = 1; k <= 2 * i - 1; k++)
                {
                    Console.Write("*" + " ");
                }
                Console.WriteLine();
            }

            Console.ReadLine();

        }

        static void Main(string[] args)
        {

            Console.WriteLine("Enter the number of rows for the output");
            int r = Convert.ToInt32(Console.ReadLine());
            print(r);
        }
       
    }
}
Output :



Previous Post Next Post