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();
           
           }


            
        }
    }

75 comments:

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

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

      Delete
    5. int num=5,x = 98,y;
      for (int i = 1; i <= num; i++)
      {
      for (int j = 1; j <= (num - i); j++)
      {
      Console.Write("_");
      }
      y=x;
      for (int k = 1; k <= i; k++)
      {
      y--;
      Console.Write(Convert.ToChar(y));
      }
      for (int l = 1; l <= (i - 1); l++)
      {
      y++;
      Console.Write(Convert.ToChar(y));
      }
      for (int m = 1; m <= (num - i); m++)
      {
      Console.Write("_");
      }
      x++;
      Console.WriteLine();
      }
      Console.ReadLine();

      Delete
  2. 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
  3. 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
  4. Try this
    For( int I=1; I<=10; I++)
    {
    For( int j=0; j<=I; j++ )
    Console.write("*" )
    Console.writeline(" ");
    }
    Console.readline();

    ReplyDelete
  5. 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
    2. class Program
      {
      static void Main(string[] args)
      {
      int num = 4, x = 65, y;
      for (int i = 1; i <= num; i++)
      {
      y = x;
      for (int j = 1; j <= i; j++)
      {
      Console.Write(Convert.ToChar(y));
      y++;
      }
      Console.WriteLine();
      }
      Console.ReadLine();
      }

      }

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

    ReplyDelete
  7. 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
  8. I Want program for this output...
    1
    13
    135

    ReplyDelete
  9. 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
  10. http://paidtorefer.org/Default.aspx?Refer=604913

    ReplyDelete
  11. 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
  12. Please help me execute this pattern in C# :( I am trying for 3 days :(
    1
    121
    12321
    1234321
    123454321
    1234321
    12321
    121
    1

    ReplyDelete
  13. 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
  14. This comment has been removed by the author.

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

    ReplyDelete
  16. 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
  17. 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
  18. 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
    2. 1
      23
      456
      78910
      i want to print this..

      Delete
  19. 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
  20. Console.Write(" ");
    what does it mean???

    ReplyDelete
  21. 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
  22. 1
    12
    123
    12
    1
    pls help me out for such type of program patern

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

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

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

    ReplyDelete
  25. 11
    11 11
    11 11 11
    11 11 11 11
    11 11 11 11 11

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

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

    ReplyDelete
  28. 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
  29. I want to print this patter
    *
    *1*
    *1*2*
    *1*2*3* and so on please tell

    ReplyDelete
  30. sir i want to print like this
    1
    2 2
    3 3 3
    4 4 4 4
    5 5 5 5 5

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

      Delete
  31. using System;
    using System.Collections.Generic;
    using System.Text;
    using sc=System.Console;
    class Program
    {
    public static void Main()
    {
    Program o = new Program();
    o.show();

    Console.ReadKey();
    }
    void show ()
    {
    int p=1,v=1;
    for (int i = 1; i <= 5; i++)
    {
    int c = v;
    for (int j = 1; j <= i; j++)
    sc.Write(c);
    sc.WriteLine();
    p = p + 1;
    v = v + 1;
    Console.WriteLine();
    Console.WriteLine();
    }
    }
    }

    ReplyDelete
  32. I was wondering how to make one like this for c#
    123454321
    1234321
    12321
    121
    1

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

    ReplyDelete
  34. i have an array {1,2,3,4,5}
    and blank space is *
    then
    i want to print
    12345
    *2345
    **345
    ***45
    ****5
    can any one tell me how can i do this?

    ReplyDelete
  35. Write a program that reads 3 numbers from users between 1 and 30 for each number that is read , your program should display the same number of adjacent asterisks . [ Using for loop ]

    ReplyDelete
  36. please write program for this....
    5
    456
    34567
    2345678
    123456789
    2345678
    34567
    456
    5

    ReplyDelete
    Replies
    1. using System;
      class Program
      {
      static void Main()
      {
      int num,start,temp,print;
      num = start = temp = 5;
      for (int i = 1; i <=(2*num-1); i++)
      {
      print = start;
      for (int j = 1; ; j++)
      {
      Console.Write(print);
      if (temp == print)
      break;
      print++;
      }
      Console.WriteLine();
      if (i < num)
      {
      start--;
      temp++;
      }
      else
      {
      start++;
      temp--;
      }
      }
      Console.ReadLine();
      }
      }

      Delete
  37. Please give me C# code for below (Diamond/Rhombus) output

    1
    2 1 2
    3 2 1 2 3
    2 1 2
    1

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

    ReplyDelete
  39. i want this type of pattern

    1 2 3 4 5 6 7 8 9 10
    36 37 38 39 40 41 42 43 44 11
    35 64 65 66 67 68 69 70 45 12
    34 63 84 85 86 87 88 71 46 13
    33 62 83 96 97 98 89 72 47 14
    32 61 82 95 100 99 90 73 48 15
    31 60 81 94 93 92 91 74 49 16
    30 59 80 79 78 77 76 75 50 17
    29 58 57 56 55 54 53 52 51 18
    28 27 26 25 24 23 22 21 20 19

    ReplyDelete
  40. Hi
    Can you answer This?
    5 4 3 2 1
    5 4 3 2
    5 4 3
    5 4
    5
    5
    5 4
    5 4 3
    5 4 3 2
    5 4 3 2 1

    ReplyDelete
  41. Hi
    Can you answer This?
    5 4 3 2 1
    5 4 3 2
    5 4 3
    5 4
    5
    5
    5 4
    5 4 3
    5 4 3 2
    5 4 3 2 1

    ReplyDelete