954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

****

hi im trying to create a C# program that'll display

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

type pattern but i can only get mine to look like this when i try to add on to the code

*
**
***
****

and then it stops. can anyone help on how im supposed to add the second part of the code??

// week 05 program 01
// creating a simple pattern of asterisks
using System;
using System.Text;
 
namespace AsterisksPattern
{
    class Asterisks
    {
        public static void Main(string[] args)
        {
           // start loop from 0
           for (int x = 0; x < 26; x++)
           {
              for (int y = 0; y <= x; y++)
              {
                 Console.Write("*");
              }
                 Console.WriteLine();
            }
                 Console.ReadLine();
        } // end Main
    } // end class
} // end of namespace
rzhaley
Newbie Poster
19 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

Hi, rzhaley, welcome on DANIWEB:)
Copy your for loops code and paste it underneath. Change the for loop with the int x in this: for (int x = 26; x > 0; x--) you start from 26 and counting down now.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

hi thanks...... i figured it was like that but im confused on which line to put this in my code wherever i try it just gives me new errors

rzhaley
Newbie Poster
19 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

Now when you watch carefully you will see the use of the same code twice. Whenever you see that you must start thining of using a method to make your code more transparant and manageable. Considr you have hunderd lines of code used More times on different places in your program. Very hard to maintain!!!
Here is what I did:

class Program
    {
        static void Main(string[] args)
        {
            // start loop from 0           
            for (int x = 0; x < 26; x++)           
            {    
                WriteStars(x);       
            }
            // start loop from 26
            for (int x = 26; x > 0; x--)
            {
                WriteStars(x);
            }
            Console.ReadLine();
        }

        //method to write a line of n stars to the console
        static void WriteStars(int n)
        {
            for (int y = 0; y <= n; y++)
            {
                Console.Write("*");
            }
            Console.WriteLine();
        }
    }
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

THANKYOU makes much more sense

rzhaley
Newbie Poster
19 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

Hi,
As a new member, b e sure to check out Member Rules . There are some guidlines in there for posting to the forums.
One of those rules is to use descriptive titles for threads. "****" doesnt tell forum users what your thread is about. By using decriptive titles you are more likely to get the help you need :)
Also, once your question has been answered, remember to mark the thread as solved.

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: