Monday, March 17, 2014

Loop Structures (While)_Ex_3_(1-7)_Wk (5)_Feb 3

Exercise 1: Display 10 stars (one per line)       
static void Main(string[] args)
        {
            int counter = 1;
            while (counter < 11)
            {
                Console.WriteLine("*");
                counter++;
            }
            Console.ReadKey();
        }
//==============================================================
Exercise 2: Request integer value N. Display N stars.
        static void Main(string[] args)
        {
            int counter = 1;
            Console.Write("Enter a value: ");
            int N = int.Parse(Console.ReadLine());
            while (counter <= N)
            {
                Console.Write("*");
                counter++;
            }
            Console.ReadKey();
        }

//==============================================================
Exercise 3: Display 10 rows of 3 stars.
        static void Main(string[] args)
        {
            int counter = 1;
            while (counter <= 10)
            {
                Console.WriteLine("***");
                counter++;
            }
            Console.ReadKey();
        }

//==============================================================
Exercise 4: Add all the values between 1 and 10 except 7.
        static void Main(string[] args)
        {
            int counter = 1;
            int sum = 0;
            while (counter < 11)
            {
                if(counter == 7)
                {
                    counter++;
                }
                else
                {
                    sum += counter;
                    Console.WriteLine("Counter: {0} Sum: {1}", counter, sum);
                    counter++;
                }
            }
            Console.ReadKey();
        }

//==============================================================
Exercise 5: Display all the even numbers between 10 and 100.
static void Main(string[] args)
        {
            int counter = 10;
            while (counter >= 10 && counter < 101)
            {
                if(counter % 2 == 0)
                {
                    Console.WriteLine("Even Number: {0}", counter);
                    counter++;
                }
                else
                {
                    counter++;
                }
            }
            Console.ReadKey();
        }
//==============================================================
Exercise 6: Add all the numbers between 10 and 100.
static void Main(string[] args)
        {
            int counter = 10;
            int sum = 0;
            while (counter >= 10 && counter < 101)
            {
                sum += counter;
                Console.WriteLine("Counter: {0} Sum: {1}", counter, sum);
                counter++;
            }
            Console.ReadKey();
        }
//==============================================================
Exercise 7: Generate 6 random numbers ranging from 1 to 50.
static void Main(string[] args)
        {
            repeat:
            Random rand = new Random();
            int counter = 1;
            while (counter <= 6)
            {
                Console.WriteLine("Random No: {0}", rand.Next(1, 51));
                counter++;
            }
            Console.ReadKey();
            Console.Clear();
            goto repeat;
        }





No comments:

Post a Comment