Monday, July 28, 2014

C# program to print patterns of numbers and stars(pyramid) .

This is a C# program to print the pyramid. This program uses the for loops to print the pyramid.

These program display Pyramid of numbers and stars. These codes illustrate how to create Pyramid using c# programming. Most of these c# programs involve usage of nested loops and space. A pattern of numbers, star or characters is a way of arranging these in some logical manner or they may form a sequence.
The trick to the triangle is to use a loop within a loop.

So, we need to get input from user, through console how many row he want to display..
In main method we will get input from user and pass that values into our print method. so, there we have 4 for loops.
just go through the source code if you have any doubts...!!
Here is source code of the C# Program to Display Number in Pyramid Shape. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below :

So here is our code :

using System;

class pyramid
{
  public static void Main()
   {
     int num , space;

      while(true)
       {
          Console.Write("Enter a number between 1 to 9 : ");

          num=Convert.ToInt32(Console.ReadLine());

          space=num-1;
       
          for(int i=1; i<=num; i++)
            {
               for(space=1; space<=(num-i); space++)
                {
                  Console.Write(" ");
                }

               for(int j=1; j<=i; j++)
                {
                  Console.Write(j);
                }
 
               for(int k=(i-1); k>=1; k--)
                {
                  Console.Write(k);
                }

              Console.WriteLine();

            }
        }

    }
}  



OUTPUT :

Enter a number between 1 to 9 : 5

      1
     121
    12321
   1234321
  123454321

How to make a pyramid using * and 'space' in C#? The output will be like this.

Here is source code of the C# Program to Display Star in Pyramid Shape. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below :
using System;

class pyramid
{
  public static void Main()
   {
     int num , space;

      while(true)
       {
          Console.Write("Enter a number between 1 to 9 : ");

          num=Convert.ToInt32(Console.ReadLine());

          space=num-1;
       
          for(int i=1; i<=num; i++)
            {
               for(space=1; space<=(num-i); space++)
                {
                  Console.Write(" ");
                }

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

              Console.WriteLine();

            }
        }

    }
}  



OUTPUT :

Enter a number between 1 to 9 : 5

      *
     ***
    *****
   *******
  *********

I'm created this program in Notepad and run in Visual Studio Command Prompt . You can use anything, Console Application or Notepad .
Now , you think which one is better ?
  • I think notepad is better , because you can learn the namespaces , syntax , methods very fast but in visual studio , it will show automatically all the namespace , syntax , methods .
How to run a program in Command Prompt ?
  • First , Type your program in notepad then save the notepad file with .csextension .
  • Then, open Visual Studio Command Prompt and select the location of file .
  • Compile the program using csc abc.cs ( Type csc (C# Compiler) and your file name ) , abc.cs ( File Name ) .
  • Execute your program , abc ( Only type your file name without extension ) .