Exclusive Clothing Sale On Soft & Soothe

Get up to 63% off on our latest collection.

Shop Now

Determine if any two integers in array sum to given integer in C#

 

Determine if any two integers in array sum to given integer

In this Article we will see any 2 number from the array sum is equal to given targeted number or not.


C# code

        static void Main(string[] args)

        {

int[] arr = new int[] { 1, 4, 2, 9, 5, 6, 3, 11 };

 

            Console.Write("Enter the Traget Number : ");

 

            int number = int.Parse(Console.ReadLine());

 

            if ( TwoIntegersSumToTarget(arr, number))

            Console.WriteLine("Given sum number exist");

           else

                Console.WriteLine("Given sum number not exist");

 

            Console.ReadLine();        }      

        public static bool TwoIntegersSumToTarget(int[] arr, int target)

        {

            for (int i = 0; i < arr.Length; i++)

            {

                for (int k = 0; k < arr.Length; k++)

                {

                    if (i != k)

                    {

                        int sum = arr[i] + arr[k];

                        if (sum == target)

                            return true;

                    }

                }

            }

            return false;

        }

output











Share this

Related Posts

Previous
Next Post »

Exclusive Clothing Sale On Soft & Soothe

Get up to 63% off on our latest collection.

Shop Now