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 ) .

34 comments:

  1. Replies
    1. {
      int i, j;
      char c;
      for (i = 0; i < num; i++)
      {
      c = 'A';
      for (j = 0; j <= i; j++)
      {
      Console.Write(c);
      c++;
      }
      Console.WriteLine(" ");
      }

      Console.ReadKey();
      }

      Delete
    2. int num, space;
      while (true)
      {
      Console.WriteLine("Please Enter No of rows");
      num = Convert.ToInt32(Console.ReadLine());
      space = num - 1;
      for (int i = 1; i <= num; i++)
      {
      for (space = 1; space <= (num - 1); 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();
      }

      }

      Delete
  2. using System;

    class program
    {
    public static void Main()
    {
    for (int i = 1; i <= 5; i++)
    {
    int no = 65;
    for (int j = 1; j <= i; j++)
    {

    char ch = (char)no;
    Console.Write(ch);
    no++;
    }

    Console.WriteLine();

    }
    Console.ReadLine();
    }
    }

    ReplyDelete
  3. 1234567890
    357924689
    83726158
    2198764
    318641
    49515
    4566
    923
    25
    7
    How to do this??

    ReplyDelete
  4. hi guys,
    i need the output in c # like this
    *
    * *
    * * *
    * * * *
    * * *
    * *
    *
    pls help me to do above pattern.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. hi i want to print the following number pattern
    1
    12
    123
    1231
    12312
    123123
    plz solve this

    ReplyDelete
    Replies
    1. int i, j;
      for (i = 1; i <=15; i++)
      {
      for (j = 1; j <=i; j++)
      {
      Console.Write("" + (j));

      }
      Console.WriteLine("");

      }
      Console.ReadKey();

      Delete
  8. This comment has been removed by the author.

    ReplyDelete
  9. 9
    9 7 9
    9 7 5 7 9
    9 7 5 3 5 7 9

    ReplyDelete
  10. 9
    9 7 9
    9 7 5 7 9
    9 7 5 3 5 7 9

    ReplyDelete
  11. how to print

    1
    123
    12345
    123
    1
    *********
    *******
    *****
    ***
    *

    ReplyDelete
  12. how to print this pattern
    *
    ***
    *****
    ***1***
    please tell me fast

    ReplyDelete
  13. How to print this pattern
    Write a C# program to generate the following pattern:
    A B C D E F G F E D C B A
    A B C D E F F E D C B A
    A B C D E E D C B A
    A B C D D C B A
    A B C C B A
    A B B A
    A A

    ReplyDelete
  14. This pattren please.........\
    12345
    21234
    32123
    43212
    54321

    ReplyDelete
  15. 1
    234
    56789
    8765432

    Give logic for this

    ReplyDelete
  16. 5
    4 4
    3 5 3
    2 4 4 2
    1 3 5 3 1
    can anyone solve this plz help me and mail to this
    mailid:kolasaicharan99@gmail.com

    ReplyDelete
  17. *
    * *
    * * *
    * * * *
    * * * * *
    * * * *
    * * *
    * *
    *

    Can anyone write a program like above

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete
  19. how can I make this:
    1
    23
    456
    78910
    and so on using user input about the end number and not rows?

    ReplyDelete
  20. How to Write
    1*2*3*10*11*13
    *4*5*8*9
    **6*7

    ReplyDelete
  21. 1
    313
    53135
    7531357
    975313579
    solve it

    ReplyDelete