コード例 #1
0
        public static void calculateSum()
        {
            //sum of elements in a list
            List <int> numSet = new List <int>();

            Console.Write("Enter No of Elements: ");
            int sizeOfList = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < sizeOfList; i++)
            {
                Console.Write("Please enter a number: ");
                numSet.Add(Convert.ToInt32(Console.ReadLine()));
            }
            //int totalSum = numSet.Sum();
            //int sum = 0;
            //foreach (int element in numSet)
            //{
            //    sum += element;
            //}
            HelperMethod Sum = new HelperMethod();
            int          sum = Sum.Sum(numSet);

            Console.WriteLine($"The sum of the numbers in the List is: {sum}");
            //Console.WriteLine($"The sum of the numbers in the List is: {totalSum}");
        }
コード例 #2
0
        private static void CalculateSumDoWhileLoop()
        {
            //sum of elements in a list using a do while loop
            List <int> numSet = new List <int>();

            Console.Write("Enter No of Elements: ");
            int sizeOfList = Convert.ToInt32(Console.ReadLine());
            int elements   = 0;

            do
            {
                Console.Write("Please enter a number: ");
                numSet.Add(Convert.ToInt32(Console.ReadLine()));
                elements++;
            } while (elements < sizeOfList);
            //int totalSum = numSet.Sum();
            //Console.WriteLine($"The sum of the numbers in the List is: {totalSum}");
            //int sum = 0;
            //foreach (int element in numSet)
            //{
            //    sum += element;
            //}
            HelperMethod Sum = new HelperMethod();
            int          sum = Sum.Sum(numSet);

            Console.WriteLine($"The sum of the numbers in the List is: {sum}");
        }
コード例 #3
0
        public static void maxvalue()
        {
            //a function that returns the largest element in a list.
            List <int> numSet = new List <int>();

            Console.Write("Enter No of Elements: ");
            int sizeOfList = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < sizeOfList; i++)
            {
                Console.Write("Please enter a number: ");
                numSet.Add(Convert.ToInt32(Console.ReadLine()));
            }
            //for list.size
            //int max = 0;
            //foreach (int element in numSet)
            //{
            //    if (max < element)
            //    {
            //        max = element;
            //    }
            //}
            //Console.WriteLine($"The Largest Element in List is: {max}");
            HelperMethod MaxSize = new HelperMethod();
            int          max     = MaxSize.MaxSize(numSet);

            Console.WriteLine($"The Largest Element in List is: {max}");
            //var max = numSet.Max();
            //Console.WriteLine($"The Largest Element in List is: {max}");
        }