Logic needed for buiding numbers pyramid

This is a discussion on Logic needed for buiding numbers pyramid within the C# Programming forums, part of the General Programming Boards category; Code: 1 121 12321 1234321 123454321 EDIT: the above pyramid looks like a doom.i.e 1-5 will be in center.1-4(L)1-3(L)1-2(L)1(L) similarly ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Location
    hyderabad
    Posts
    2

    Unhappy Logic needed for buiding numbers pyramid

    Code:
         1
        121
       12321
      1234321
     123454321
    EDIT:
    the above pyramid looks like a doom.i.e 1-5 will be in center.1-4(L)1-3(L)1-2(L)1(L) similarly 1-4(R) 1-3(R) 1-2(R) 1(R)

    I'm partly successful in building the requirement of the program.but cant able to think the logic for the other half.please tell me how can i proceed.below is the code written in c#.

    Code:
                for (int i = 1; i <= 5; i++)
                {
                    //int l = 1;
                    int k = 1;
                    for (int j = 5; j >=1; j--)
                    {
                        if (j > i)
                        {
                            Console.Write(" ");
                        }
                        else
                        {
                            Console.Write(k++);
                        }
                     
                       
                    }
    
                    Console.WriteLine();
                   
                }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
        1
       1
      1
     1
    1
    Ok. If your number is 5, then you have 5-centernumber spaces to the left (and to the right)

    So you write out that many space.
    Then you write numbers from 1 to centernumber-1.
    Then you write the center number.
    Then you write numbers from centernumber-1 to 1.

    All of that is one line. For each line from 1 to number, do all of that, and at the start of the next line, increment the center number.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Location
    hyderabad
    Posts
    2
    Quote Originally Posted by quzah View Post
    Code:
        1
       1
      1
     1
    1
    Ok. If your number is 5, then you have 5-centernumber spaces to the left (and to the right)

    So you write out that many space.
    Then you write numbers from 1 to centernumber-1.
    Then you write the center number.
    Then you write numbers from centernumber-1 to 1.

    All of that is one line. For each line from 1 to number, do all of that, and at the start of the next line, increment the center number.


    Quzah.
    thanks for the response.i'll try this.

  4. #4
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    I have a formular, that doesn't need to count for each number. (it means if you know n lines: line i like y_coor, at line i, j(th) like x_coor, so I know what number at coordinate(x,y). But maybe It is not nescessary. Here is my straightforward code same with quxah idea:
    Code:
    namespace Program
    {
        class Program
        {
            static void Main(string[] args)
            {
                // print triangle
                int n = 5;
                /* three phrase: 
                 * first: find first location of the line
                 * second: print increasing
                 * third: print decreasing */
                int k=n;
                for (int i = 0; i <n; i++)  //print n line
                {
                    //  first
                    for (int j = 1; j <= k; j++) Console.Write(" ");
                    // second
                    for (int j = 1; j <= i; j++) Console.Write(j);
                    // third
                    for (int j = i + 1; j >= 1; j--) Console.Write(j);
                    k--;
                    Console.WriteLine();
                }
     
            }
        }
    }

    @: Oh, I'm sorry, the lastest post has appeared last week. I just post a new one because I just want to practice C# code.
    Last edited by hqt; 01-18-2012 at 04:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Digits pyramid printing programs needed in C Programming
    By technoexam in forum C Programming
    Replies: 8
    Last Post: 10-01-2011, 05:58 PM
  2. Fining the mode in a vector of numbers: Logic errors
    By pantera in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2009, 07:43 PM
  3. Replies: 1
    Last Post: 11-02-2009, 06:45 AM
  4. Some logic with prime-numbers
    By kenryuakuma in forum C++ Programming
    Replies: 11
    Last Post: 12-15-2008, 09:38 AM
  5. Help needed with VERY large numbers.
    By jwarner in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2004, 12:01 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21