Monday, March 17, 2014

Mod function and division_Ex_2 (Wk 5)_Feb_4

Question: Generate 20 random values between 1 and 10, add only the ones that are odd. Display the sum.

        static void Main(string[] args)
        {
        repeat:
            Random rand = new Random();
            int counter = 1;
            int oddsum = 0;
            while (counter <= 20)
            {
                int rdValue = rand.Next(1, 11);
                if (rdValue % 2 != 0)
                {
                    oddsum += rdValue;
                    counter++;
                }
                else
                {
                    counter++;
                }
                Console.WriteLine("Random value are: {0}", rdValue);
            }
            Console.WriteLine("\nOdd sum is: {0}", oddsum);
            Console.WriteLine("\nHit enter to continue");
            Console.ReadKey();
            Console.Clear();
            goto repeat;
        }

No comments:

Post a Comment