Monday, March 17, 2014

Mod function and division_Ex_1 (Wk 5)_Feb_4

Question: Generate a random value from -100 to 100 then indicates if the value is positive or negative and even or odd. Use Random and Mod.
        static void Main(string[] args)
        {
            repeat:
            Random rand = new Random();
            int rdValue = rand.Next(-100, 101);

            Console.WriteLine("Random Value is: {0}", rdValue);

            if (rdValue > 0 && (rdValue % 2 == 0))
            {
                Console.WriteLine("Value is Positive and Even");
            }
            else if (rdValue > 0 && (rdValue % 2 != 0))
            {
                Console.WriteLine("Value is Positive and Odd");
            }
            else if (rdValue < 0 && (rdValue % 2 == 0))
            {
                Console.WriteLine("Value is Negative and Even");
            }
            else
            {
                Console.WriteLine("Value is Negative and Odd");
            }
            Console.WriteLine("Hit enter to continue");
            Console.ReadKey();
            Console.Clear();
            goto repeat;
        }

No comments:

Post a Comment