Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 4 - Subtract the largest even number and smallest odd number in the given array elements

Suggested Videos:
Part 1 - How to retrieve data from different databases in asp.net
Part 2 - Display database table metadata in asp.net web application
Part 3 - How to bind a computed column to GridView



One of our YouTube channel subscribers faced this question in a C# written testWrite a program in c# to subtract the largest even number and smallest odd number in the given array elements. This has to be achieved without using LINQ. 



The output of the program should be as shown below.
 C# program to subtract the largest even number and smallest odd number in the given array elements

using System;
namespace Demo
{
    class Program
    {
        static void Main()
        {
            int?[] Numbers = { 555, 3, 1, 20, 1000 };

            int? largestEvenNumber = null;
            int? smallestOddNumber = null;

            foreach (int i in Numbers)
            {
                if ((i % 2) == 0)
                {
                    if (!largestEvenNumber.HasValue || i > largestEvenNumber)
                    {
                        largestEvenNumber = i;
                    }
                }
                else
                {
                    if (!smallestOddNumber.HasValue || i < smallestOddNumber)
                    {
                        smallestOddNumber = i;
                    }
                }
            }

            if (largestEvenNumber == null)
            {
                Console.WriteLine("Array does not contain any even number");
            }
            else
            {
                Console.WriteLine("Largest even number = " + largestEvenNumber);
            }

            if (smallestOddNumber == null)
            {
                Console.WriteLine("Array does not contain any odd number");
            }
            else
            {
                Console.WriteLine("Smallest odd number = " + smallestOddNumber);
            }

            if (largestEvenNumber != null && smallestOddNumber != null)
            {
                Console.WriteLine("{0} - {1} = {2}", largestEvenNumber,
                    smallestOddNumber, (largestEvenNumber - smallestOddNumber));
            }
        }
    }
}

The logic for finding the largest even number and smallest odd number becomes much easier when we use the following LINQ functions.
1. Where()
2. Max()
3. Min()

using System;
using System.Linq;
namespace Demo
{
    class Program
    {
        static void Main()
        {
            int?[] Numbers = { 555, 3, 1, 20, 1000 };

            int? largestEvenNumber = Numbers.Where(x => x % 2 == 0).Max();
            int? smallestOddNumber =
                          smallestOddNumber = Numbers.Where(x => x % 2 != 0).Min();

            if (largestEvenNumber == null)
            {
                Console.WriteLine("Array does not contain any even number");
            }
            else
            {
                Console.WriteLine("Largest even number = " + largestEvenNumber);
            }

            if (smallestOddNumber == null)
            {
                Console.WriteLine("Array does not contain any odd number");
            }
            else
            {
                Console.WriteLine("Smallest odd number = " + smallestOddNumber);
            }

            if (largestEvenNumber != null && smallestOddNumber != null)
            {
                Console.WriteLine("{0} - {1} = {2}", largestEvenNumber,
                    smallestOddNumber, (largestEvenNumber - smallestOddNumber));
            }
        }
    }
}

dot net written test questions answers

No comments:

Post a Comment

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.