Tuesday, March 18, 2014

Do_While_Loop_Ex_5 (Wk-7) Feb 17-21, 2014

Ex 5: Add all the consecutive values starting at 100 until the sum exceeds 10000. Display the final value of sum. Display also the number of values used.

        static void Main(string[] args)
        {
            int counter = 100;
            int sum = 0;

            do
            {
                sum += counter;
                Console.WriteLine("value No:{0}, sum:{1}", counter, sum);
                counter++;
            } while (sum <= 10000);
            Console.WriteLine("\nThe final sum is: {0}", sum);
            Console.WriteLine("\nThe number of values used are: {0}", counter - 100);
            Console.ReadLine();
        }

No comments:

Post a Comment