Friday, 30 June 2017

Find all Pairs in Array of Integers whose Sum is equal to a given Number


        static void Main(string[] args)
        {
            int[] arr = { 3,1,2,2,3,0,4,2,6 };
            int num = 5;
            for (int i = 0; i < 9; i++)
            {
                for (int j = i + 1; j < 9; j++)
                {
                    if (num == arr[i] + arr[j])
                    {
                        Console.WriteLine(arr[i] + "," + arr[j]);
                    }
                }
            }
            Console.ReadLine();
        }

Output :

No comments:

Post a Comment