Tuesday, March 18, 2014

LoopStructure_Ex_5 (Wk 6) Feb 14, 2014

Ex 5: You have two jars A and B. Jar A contains 1000 marbles. Jar B contains 300 marbles. Everyday, for 39 day, you take few marbles from Jar A and put them in jar B. On odd days you takes 13 marbles and on even days you take 7 marbles. Determine the number of marbles each jar has after 39 days. (use for loop).

static void Main(string[] args)
        {
            int JarA = 1000;
            int JarB = 300;
            for (int day = 1; day <= 39; day++)
            {
                if (day % 2 == 0)
                {
                    JarA -= 7;
                    JarB += 7;
                }
                else
                {
                    JarA -= 13;
                    JarB += 13;
                }
                Console.WriteLine("{0, -2} day, Jar A {1, -4} Jar B {2}", day, JarA, JarB);
            }
            Console.WriteLine("After 39 days, Jar A {0} Jar B {1}", JarA, JarB);
            Console.ReadKey();
        }

No comments:

Post a Comment