Saturday 13 December 2014

Printing a sequence with out using multiple loops


Sequence

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

How to do it

using System;
using System.Collections;
 
namespace PrintStarSequence
{
    class Program
    {
        static ArrayList arrayList = null;
        static void Main(string[] args)
        {
            int countofMaxStars= 5;
            int start =1;
            int counter = 1;
            bool taskDone = false;
            bool isReverse = false;
            arrayList = new ArrayList();
 
            do
            {
                if(isReverse == true)
                {
                    arrayList.Remove("*");
                    Console.Write(String.Join(String.Empty, arrayList.ToArray()));
                    countofMaxStars--;
                    if(countofMaxStars < start)
                        taskDone = true;
                    
                }
                else
                {
                    arrayList.Add("*");
                    Console.Write(String.Join(String.Empty, arrayList.ToArray()));
                    if (counter == countofMaxStars)
                    { 
                        isReverse = true;
                        counter = 1;
                        countofMaxStars--;
                    }
 
                }
 
                Console.WriteLine();
                counter++;
 
 
            } while (taskDone == false);
 
            Console.Read();
        }
 
        
    }
 
}

No comments:

Post a Comment