Wednesday, August 1, 2012

Piramid printing...

One of my friend asked me to do a homework 4 him, ie to display alphabets in a piramid mode like



I ve made a console application with the following codes.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProgramContest
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.printFn();
        }

        private void printFn()
        {
            string[] ar = new string[] {"A","B","C","D","E","F" };

            int len = ar.Length - 1;
            while (!string.IsNullOrEmpty(ar[0]))
            {               
                foreach (string s in ar)
                {
                    Console.Write(s != string.Empty ? s : " ");
                }

                for (int i = ar.Length - 2; i >= 0; i--)
                {
                    Console.Write(ar[i] != string.Empty ? ar[i] : " ");
                }

                Console.Write(Environment.NewLine);
                ar[len] = string.Empty;
                len--;
            }

            Console.Write(Environment.NewLine);
            Console.Write(Environment.NewLine);
            Console.WriteLine("press any key to exit");
            ConsoleKeyInfo k= Console.ReadKey();
            if (k.ToString()!=string.Empty)
            {
                Environment.Exit(0);
            }
        }
    }
}

1 comment: