Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   c# code for a pattern (http://www.go4expert.com/forums/c-sharp-code-pattern-t27524/)

naziasarguru 3Jan2012 18:55

c# code for a pattern
 
somebody help me out with the c# program code which prints the following output:
1
0 1
1 0 1
0 1 0 1

where the height of the triangle is taken as command line argument.

Ranzan 4Jan2012 13:18

Re: c# code for a pattern
 
This will help you:

Code:

            StringBuilder sb = new StringBuilder();
            //height of triangle 
            int height = 5;
            bool latestZeroWritten = false;
            for (int i = 1; i <= height; i++)
            {
                //decides whether next line starts with 1
                latestZeroWritten = !(i % 2 == 0);

                for (int j = 1; j <= i; j++)
                {
                    if (latestZeroWritten)
                    {
                        sb.Append("1 ");
                        latestZeroWritten = false;
                    }
                    else
                    {
                        sb.Append("0 ");
                        latestZeroWritten = true;
                    }

                }
                sb.Append(Environment.NewLine);
            }
            MessageBox.Show(sb.ToString());



All times are GMT +5.5. The time now is 22:43.