Monday, March 17, 2014

Loop Structures (While)_Ex_5_Wk (5)_Feb 4

Question: Add all the consecutive integers from 100 to 200 except the one that are in the 130 to 170 range.
static void Main(string[] args)
        {
            long total = 0;
            int counter = 100;
            while (counter <= 200)
            {
                if(counter < 130 || counter > 170)
                {
                    total += counter;
                    counter++;
                }
                else
                {
                    counter++;
                }
            }
            Console.WriteLine("Total Value: {0}", total);
            Console.ReadKey();
        }

No comments:

Post a Comment