Distinct pair of numbers from an integer array whose product is equal to a given number

using System;

using System.Collections.Generic;

using System.Text;

using System.Collections;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            int[] numArray = { 1, 1, 2, 2, 0, 3, 2, 2, 6, 6, 1, 1, 3 };

            int givenNumber = 0;

            List<int> numList = new List<int>();

            //Finding all the distinct pair of numbers from an integer array whose product is equal to the given number

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

            {

                for (int j = 0; j < numArray.Length; j++)

                {

                    if (i != j)

                    {

                        if (numArray[i] * numArray[j] == givenNumber)

                        {

                            if (!numList.Contains(numArray[i]))

                            {

                                if (numArray[i] == numArray[j])

                                {

                                    numList.Add(numArray[i]);

                                }

                                else

                                {

                                    numList.Add(numArray[i]);

                                    numList.Add(numArray[j]);

                                }

                                Console.WriteLine(numArray[i] + "*" + numArray[j]);

                            }

                        }

                    }

                }

            }

        }

    }

}
 
1*0
2*0
3*0
6*0
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s