Console.WriteLine("Enter array values with seperation : "); string abc = Console.ReadLine();int[] arr = new int[abc.Split(':').Length];string[] strArray = abc.Split(':');for (int i=0; i < strArray.Length;i++ ){arr[i] = Convert.ToInt32(strArray[i].ToString());}Console.WriteLine("Enter sum of number : "); int sumofinput = Convert.ToInt32(Console.ReadLine()); for (int i = 0; i < arr.Length; i++) { for (int j = 0; j < arr.Length; j++) { if (Convert.ToInt32(arr[i]) + Convert.ToInt32(arr[j]) == sumofinput)Console.WriteLine("Sum of " + sumofinput + " pair is :" + arr[i] + "," + arr[j]);} } Console.Read();
public static int[] ArraySum(int[] arr,int sum) { int len = arr.Length; int[] sumArr = null; for (int i = 0; i <; len; i++) { for (int j = 0; j <; len; j++) { if (i != j) { if (arr[i] + arr[j] == sum) { sumArr = new int[2]; sumArr[0] = arr[i]; sumArr[1] = arr[j]; break; } } } if (sumArr != null) break; } return sumArr; }
public static int[] ArraySum(int[] arr,int sum)
{
int len = arr.Length;
int[] sumArr = null;
for (int i = 0; i <; len; i++)
for (int j = 0; j <; len; j++)
if (i != j)
if (arr[i] + arr[j] == sum)
sumArr = new int[2];
sumArr[0] = arr[i];
sumArr[1] = arr[j];
break;
}
if (sumArr != null) break;
return sumArr;
Console.WriteLine("Enter max of array : ");int[] arr = new int[Convert.ToInt32(Console.ReadLine())];for (int i = 0; i < arr.Length; i++){arr[i] = Convert.ToInt32(Console.ReadLine());}Console.WriteLine("Enter sum of number : ");int sumofinput = Convert.ToInt32(Console.ReadLine());for (int i = 0; i < arr.Length; i++){for (int j = 0; j < arr.Length; j++){if (Convert.ToInt32(arr[i]) + Convert.ToInt32(arr[j]) == sumofinput)Console.WriteLine("Sum of " + sumofinput + " pair is :" + arr[i] + "," + arr[j]);}}Console.Read();
static bool findPairFromArray(int[] v,int suma,int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n && j != i; j++) { if (v[i] + v[j] == suma) { Console.WriteLine("The pair is :[{0},{1}]", v[i], v[j]); return true; } } } return false; } static void Main(string[] args) { int[] v ={ 1, 2, 4, 7, 8,6,3 }; //findPairFromArray(v, 8, 7); if (findPairFromArray(v, 10, 7)) { Console.WriteLine("I find a pair!"); } Console.ReadKey(); }}
static bool findPairFromArray(int[] v,int suma,int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n && j != i; j++) {
if (v[i] + v[j] == suma) {
Console.WriteLine("The pair is :[{0},{1}]", v[i], v[j]);
return true;
return false;
static void Main(string[] args)
int[] v ={ 1, 2, 4, 7, 8,6,3 };
//findPairFromArray(v, 8, 7);
if (findPairFromArray(v, 10, 7))
Console.WriteLine("I find a pair!");
Console.ReadKey();
public static void addaraay(){int[] addnum = { 2, 1, 6, 4, 8, 3, 2, 1 };string sum = Console.ReadLine();int totalsum = Convert.ToInt32(sum);int[] addnum1 = addnum;for (int i = 0; i <= addnum.Length - 1; i++){int j = i + 1;for (; j < addnum1.Length - 1; j++){ int howmuch = addnum[i] + addnum1[j]; if (howmuch == totalsum) { Console.WriteLine("pair is " + addnum[i] +", " +addnum1[j]); } } }
public static void addaraay()
int[] addnum = { 2, 1, 6, 4, 8, 3, 2, 1 };
string sum = Console.ReadLine();
int totalsum = Convert.ToInt32(sum);
int[] addnum1 = addnum;
for (int i = 0; i <= addnum.Length - 1; i++)
int j = i + 1;
for (; j < addnum1.Length - 1; j++)
int howmuch = addnum[i] + addnum1[j];
if (howmuch == totalsum)
Console.WriteLine("pair is " + addnum[i] +", " +addnum1[j]);
int[] x = { 1, 2, 3, 6, 8, 1, 6 };for (int i = 0; i < x.Length; i++){for (int j = 0; j < x.Length; j++){if (5 == x[i] + x[j]){}}}
Console.WriteLine("Enter array values with seperation , ");string abc = Console.ReadLine();int[] arr = new int[abc.Split(',').Length];string[] strArray = abc.Split(',');for (int i = 0; i < strArray.Length; i++){arr[i] = Convert.ToInt32(strArray[i].ToString());}Console.WriteLine("Enter sum of number : ");int sumofinput = Convert.ToInt32(Console.ReadLine());//int[] SumnMinusOne for (int i = 0; i < arr.Length; i++){int ComplementaryNumber = sumofinput - arr[i];if (arr.Contains(ComplementaryNumber)){Console.WriteLine("Sum of " + sumofinput + " pair is :" + arr[i] + "," + ComplementaryNumber);break;}}
sort array set 2 pointer (p1,p2) to first and end of array loop while p1 smaller than p2 check if p1+p2 is equla to number return true check if p1+p2 is greater than number decrease p2 check if p1+p2 is equla to number increase p1