Friday, March 21, 2014

Array with Method_Ex_1 (Wk 8) Feb 24-28, 2014

Q1. Method to count the number of odd values this array has.

        static void Main(string[] args)
        {
            int[] A = { 12, -5, -9, 3, 6, 19, -13, -22, 33, 1, -17, 29 };
            //Display the array in single line as it is.
            for (int i = 0; i < A.Length; i++)
            {
                Console.Write("{0}, ", A[i]);
            }
            //=======================================================
            //Call the method
            int oddnum = OddCounter(A);
            Console.WriteLine("\n\nThere are {0} odd values in this array", oddnum);

            Console.ReadLine();
        }
        //Method to count the number of odd values this array has.
        static int OddCounter(int[] A)
        {
            int counter = 0;
            for (int i = 0; i < A.Length; i++)
            {
                if (A[i] % 2 != 0)
                    counter++;
            }
            return counter;
        }
    }
}



No comments:

Post a Comment