Showing posts with label Array Interview questions. Show all posts
Showing posts with label Array Interview questions. Show all posts

Determine if any two integers in array equal to sum of given number




//------------------------------

1.     
  Determine if any two integers in array equal to sum of given number


        public static string CheckArraySum(int[] ar, int found)

        {
            for (int i = 0; i <= ar.Length - 1; i++)
            {
                for (int j = i+1; j<= ar.Length - 1; j++)
                {
                    if (ar[i] + ar[j] == found)
                    {
                        return "Sum Exist ! Values are " + ar[i] + " " + ar[j];
                    }
                }
            }

            return " Value "+ found +"Not in array ";
        }
      
//-----------------------------------------------------------------------------------

        static void Main(string[] args)
        {
 Console.WriteLine("Determine if any two intergers in array equal to sum of given number \n");

            Console.WriteLine("Enter array Size");
            int arraysize = Convert.ToInt32(Console.ReadLine());
            int[] arr = new int[arraysize];

            Console.WriteLine("Enter the array Value");
            for (int i = 0; i <= arraysize - 1; i++)
            {
                arr[i]= Convert.ToInt32(Console.ReadLine());
            }

            Console.WriteLine("Enter the value you want find");

            int val = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Result"+ Program.CheckArraySum(arr, val));
            Console.ReadLine();
        }
    
Share:

Popular

Tags

Mobile

Recent Posts