Tuesday, 16 October 2012

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


These program prints various different patterns of numbers and stars. These codes illustrate how to create various patterns 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. Some of these patterns are triangles which have special importance in mathematics. Some patterns are symmetrical while other are not. Please see the complete page and look at comments for many different patterns.
    *
   ***
  *****
 *******
*********
We have shown five rows above, in the program you will be asked to enter the numbers of rows you want to print in the pyramid of stars.

C programming code

class Program
    {
        static void Main(string[] args)
        {
            int row,c,n,temp;
            n = 5;
           temp= n ;
           for (row = 1; row <= n; row++)
           {
               for (c = 1; c < temp; c++)
                   Console.Write(" ");
               
               temp--;
               for (c = 1; c <= 2 * row - 1; c++)
                   Console.Write("*");

               Console.WriteLine();
           
           }


            
        }
    }

57 comments:

  1. Very informative post indeed.. being enrolled in http://www.wiziq.com/course/6314-learn-c-programming-language-low-priced-student-edition was looking for such articles online to assist me.. and your post helped me a lot

    ReplyDelete
  2. i want to print this format in c#
    * *
    ** **
    *** ***
    **** ****
    **********

    but it print this
    *
    **
    ***
    ****
    *****
    *
    **
    ***
    ****
    *****

    i am writing this code:-

    int number = 5;
    int i, j, k;
    for (i = 1; i <= number; i++)
    {
    for (j = 1; j <= number - i; j++)
    {
    Console.Write("");
    }
    for (k = 1; k <= i; k++)
    {
    Console.Write("*");
    }
    Console.WriteLine("");
    }
    for (i = 1; i <= number; i++)
    {
    for (j = 1; j <= number - i; j++)
    {
    Console.Write(" ");
    }
    for (k = 1; k <= i; k++)
    {
    Console.Write("*");
    }
    Console.WriteLine("");
    }
    Console.ReadLine();

    ReplyDelete
    Replies
    1. class Program
      {
      static void Main(string[] args)
      {

      string str = "*";

      for (int i = 1; i <= 5; i++)
      {
      for (int j = 1; j <= i; j++)
      {
      Console.Write("*");

      }
      Console.Write(" ");
      for (int m = 1; m <= i; m++)
      {
      Console.Write("*");
      }



      Console.WriteLine();


      }

      Console.ReadLine();

      }

      }
      }

      Delete
    2. hi Ms Durga,

      i want to print lyk this
      ____a____
      ___bab___
      __cbabc__
      _dcbabcd_
      edcbabcde

      can u just hlp me out how do i print this in c# ?
      Regards,
      Hiren

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

    ReplyDelete
    Replies
    1. Try this...

      int num = 5;

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

      if (i != num - 1)
      {
      Console.Write(" ");
      }
      else
      {
      Console.Write(" * ");
      }

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

      Console.ReadLine();

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

    ReplyDelete
    Replies
    1. hi Ms Surani,

      i want to print lyk this
      ____a____
      ___bab___
      __cbabc__
      _dcbabcd_
      edcbabcde

      can u just hlp me out how do i print this in c# ?
      Regards,
      Hiren

      Delete
    2. hi Ms Surani,

      i want to print lyk this
      ____a____
      ___bab___
      __cbabc__
      _dcbabcd_
      edcbabcde

      can u just hlp me out how do i print this in c# ?
      Regards,
      Hiren

      Delete
  5. Try this
    For( int I=1; I<=10; I++)
    {
    For( int j=0; j<=I; j++ )
    Console.write("*" )
    Console.writeline(" ");
    }
    Console.readline();

    ReplyDelete
  6. I want to print:
    A
    A B
    A B C
    A B C D

    How to do this?

    ReplyDelete
    Replies
    1. class Program
      {
      static void Main(string[] args)
      {

      string str = "ABCD";
      int m=str.Length;

      for (int i = 0; i < str.Length; i++)
      {
      for (int j = 0; j <= i; j++)
      {

      Console.Write(str[m-(m-j)]);

      }
      Console.WriteLine();
      }

      Console.ReadLine();

      }

      }

      is there any other solution for this related task.plse share it to kdrnlr@gmail.com

      Delete
  7. If i want to make the pyramid with the help of foreach loop then it is possible or not???

    ReplyDelete
  8. i want to print to like 1
    1 2 1
    1 2 3 2 1
    1 2 3 4 3 2 1



    how do this pls reply

    ReplyDelete
    Replies
    1. have you got the code for that. If so, Please share with me at hassansafeer.sbi@gmail.com
      1
      121
      12321
      1234321
      123454321
      1234321
      12321
      121
      1

      Delete
  9. I Want program for this output...
    1
    13
    135

    ReplyDelete
  10. i want to print a pattern using for and while loop in c# like this
    ***S******
    ******S***
    ***S******
    ******S***
    ***S******
    ******S***

    how to do it? help please =(

    ReplyDelete
    Replies
    1. oye lun tjhy mil gay yeh pattern ???

      Delete
    2. I want Program for this output
      999999999
      12345678
      7777777
      123456
      55555
      1234
      333
      12
      1

      Delete
    3. using System;

      class pyramid {

      static void Main() {

      /** Pyramid stars Looking down
      Comment this if u need only a upside pyramid **/

      int row, i, j;

      // Total number of rows
      // You can get this as users input
      //row = Int32.Parse(Console.ReadLine());
      row = 5;

      // To print odd number count stars use a temp variable
      int temp;
      temp = row;

      // Number of rows to print
      // The number of row here is 'row'
      // You can change this as users input
      for ( j = 1 ; j <= row ; j++ ) {

      // Printing odd numbers of stars to get
      // Number of stars that you want to print with respect to the value of "i"?
      for ( i = 1 ; i <= 2*temp - 1 ; i++ )
      Console.Write("*");

      // New line after printing a row
      Console.Write("\n");
      for ( i = 1 ; i <= j ; i++ )
      Console.Write(" ");

      // Reduce temp value to reduce stars count
      temp--;
      }

      /** Pyramid stars Looking up
      Comment this if u need only a downside pyramid **/
      int rowx, k, l;

      // Total number of rows
      // You can get this as users input
      // rowx = Int32.Parse(Console.ReadLine());
      rowx = 5;

      // To print odd number count stars use a temp variable
      int tempx;
      tempx = rowx;

      //Remove this if u use
      Console.Write("\n");

      // Number of rows to print
      // The number of row here is 'rowx'

      for ( l = 1 ; l <= rowx ; l++ ) {

      // Leaving spaces with respect to row
      for ( k = 1 ; k < tempx ; k++ )
      Console.Write(" ");

      // Reduce tempx value to reduce space(" ") count
      tempx--;

      // Printing stars
      for ( k = 1 ; k <= 2*l - 1 ; k++ )
      Console.Write("*");

      // New line after printing a row
      Console.Write("\n");
      }
      }
      }

      Delete
  11. http://paidtorefer.org/Default.aspx?Refer=604913

    ReplyDelete
  12. Plz tell me to logic of this code
    ******
    *****
    ****
    ***
    **
    *

    ReplyDelete
    Replies
    1. using System;

      class pyramid {

      static void Main() {

      /** Pyramid stars Looking down
      Comment this if u need only a upside pyramid **/

      int row, i, j;

      // Total number of rows
      // You can get this as users input
      //row = Int32.Parse(Console.ReadLine());
      row = 5;

      // To print odd number count stars use a temp variable
      int temp;
      temp = row;

      // Number of rows to print
      // The number of row here is 'row'
      // You can change this as users input
      for ( j = 1 ; j <= row ; j++ ) {

      // Printing odd numbers of stars to get
      // Number of stars that you want to print with respect to the value of "i"?
      for ( i = 1 ; i <= 2*temp - 1 ; i++ )
      Console.Write("*");

      // New line after printing a row
      Console.Write("\n");
      for ( i = 1 ; i <= j ; i++ )
      Console.Write(" ");

      // Reduce temp value to reduce stars count
      temp--;
      }

      /** Pyramid stars Looking up
      Comment this if u need only a downside pyramid **/
      int rowx, k, l;

      // Total number of rows
      // You can get this as users input
      // rowx = Int32.Parse(Console.ReadLine());
      rowx = 5;

      // To print odd number count stars use a temp variable
      int tempx;
      tempx = rowx;

      //Remove this if u use
      Console.Write("\n");

      // Number of rows to print
      // The number of row here is 'rowx'

      for ( l = 1 ; l <= rowx ; l++ ) {

      // Leaving spaces with respect to row
      for ( k = 1 ; k < tempx ; k++ )
      Console.Write(" ");

      // Reduce tempx value to reduce space(" ") count
      tempx--;

      // Printing stars
      for ( k = 1 ; k <= 2*l - 1 ; k++ )
      Console.Write("*");

      // New line after printing a row
      Console.Write("\n");
      }
      }
      }

      Delete
    2. //32.================================================
      using System;
      class program
      {
      public static void Main()
      {
      Console.WriteLine("Program for displaying Pattern of '*'.");
      Console.Write("Enter the maximum number of '*': ");
      int n = Convert.ToInt32(Console.ReadLine());

      Console.WriteLine("Here is the Pattern of Stars");

      for (int i = 0; i <= n; i++)
      {
      for (int j = i; j <= n - 1; j++)
      Console.Write("*");
      Console.WriteLine();
      }
      Console.ReadLine();
      }
      }


      Delete
  13. Please help me execute this pattern in C# :( I am trying for 3 days :(
    1
    121
    12321
    1234321
    123454321
    1234321
    12321
    121
    1

    ReplyDelete
  14. Please help me execute this pattern in C# :( I am trying for 3 days :(
    1
    121
    12321
    1234321
    123454321
    1234321
    12321
    121
    1


    Please E-mail me at hassansafeer.sbi@gmail.com

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. for java programmers use this code to get the above output-----


      package webdriver_project;

      public class forloop {

      public static void main(String[] args) {
      int i;
      int j;
      int k;
      for(i=1;i<=5;i++){
      for(j=1;j<=i;j++){
      System.out.print(j);
      }
      for(k=i;k>1;k--){
      System.out.print(k-1);
      }
      System.out.println("");
      }
      for(i=4;i>=1;i--){
      for(j=1;j<=i;j++){
      System.out.print(j);
      }
      for(k=i;k>1;k--){
      System.out.print(k-1);
      }
      System.out.println("");
      }
      }
      }

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

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

    ReplyDelete
  17. I want to print :
    1
    21
    321
    4321

    Please reply me

    ReplyDelete
    Replies
    1. using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;

      namespace ArshLogicalProg
      {
      class Patternv
      {
      static void Main()
      {

      for (int i = 1; i <= 4; i++)
      {
      for (int j = i; j >= 1; j--)
      {

      Console.Write(j);
      }
      Console.WriteLine();
      }
      Console.ReadLine();
      }
      }
      }

      Delete
  18. i want to print lyk this
    a
    bab
    cbabc
    dcbabcd
    edcbabcde

    can u just hlp me out how do i print this in c#

    ReplyDelete
  19. 1
    2 3
    4 5 6
    7 8 9 10
    plz any one help to print this

    ReplyDelete
    Replies
    1. using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;

      namespace ArshLogicalProg
      {
      class Patternv
      {
      static void Main()
      {
      int a = 1;
      for (int i = 1; i <= 4; i++)
      {
      for (int j = 1; j <= i; j++)
      {

      Console.Write(a);
      a++;

      }
      Console.WriteLine();
      }
      Console.ReadLine();
      }
      }
      }

      Delete
  20. i want to print this
    1
    2 2
    3 3 3
    4 4 4 4
    5 5 5 5 5

    ReplyDelete
    Replies
    1. using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;

      namespace ArshLogicalProg
      {
      class Patternv
      {
      static void Main()
      {

      for (int i = 1; i <= 4; i++)
      {
      for (int j = 1; j <= i; j++)
      {

      Console.Write(i);


      }
      Console.WriteLine();
      }
      Console.ReadLine();
      }
      }
      }

      Delete
    2. in java------------



      package webdriver_project;

      public class firstjavaclass {
      public static void main(String[] args){
      int i=1;
      int j=1;

      for(i=1;i<=5;i++){
      for(j=1;j<=i;j++){
      System.out.print(i);
      }
      System.out.println();

      }
      }}

      Delete
  21. Console.Write(" ");
    what does it mean???

    ReplyDelete
  22. hi, I want to print this this in c#.how i write?

    If user input is 2
    Result is :
    *
    ***
    *****
    * *
    *** ***
    ***** *****

    If user input is 3
    Result is :
    *
    ***
    *****
    * *
    *** ***
    ***** *****
    * * *
    *** *** ***
    ***** ***** *****

    If user input is 4
    Result is :
    *
    ***
    *****
    * *
    *** ***
    ***** *****
    * * *
    *** *** ***
    ***** ***** *****
    * * * *
    *** *** *** ***
    ***** ***** ***** *****

    ReplyDelete
  23. 1
    12
    123
    12
    1
    pls help me out for such type of program patern

    ReplyDelete
  24. Hi...could someone help me print this in c#...thanks

    1****
    *2***
    **3**
    ***4*
    ****5

    ReplyDelete
  25. can pls tell tis..
    *
    ##
    ***
    ####

    ReplyDelete
  26. 11
    11 11
    11 11 11
    11 11 11 11
    11 11 11 11 11

    ReplyDelete
  27. 1 1
    2 3 2 3
    4 5 6 4 5 6
    7 8 9 10 9 8 7

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

    ReplyDelete
  29. ankush this is the code you have to write for your program--
    package webdriver_project;

    public class firstjavaclass {
    public static void main(String[] args){
    int i=1;
    int j=1;
    for(i=1;i<=5;i++){

    System.out.println("");
    for(j=1;j<=i;j++){
    System.out.print("*");

    }
    System.out.print(" ");
    for(j=i;j>=1;j--){
    System.out.print("*");



    }
    }
    }
    }






    having output---
    * *
    ** **
    *** ***
    **** ****
    ***** *****

    ReplyDelete
  30. I want to print this patter
    *
    *1*
    *1*2*
    *1*2*3* and so on please tell

    ReplyDelete