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

CsharpProgrammingServer Side Programming

#### Complete Python Prime Pack

9 Courses     2 eBooks

#### Artificial Intelligence & Machine Learning Prime Pack

6 Courses     1 eBooks

#### Java Prime Pack

9 Courses     2 eBooks

The following is our array −

int[] arr = new int[] {
7,
4,
6,
2
};

Let’s say the given intger that should be equal to sum of two other integers is −

int res = 8;

To get the sum and find the equality.

for (int i = 0; i < arr.Length; i++) {
for (int j = 0; j < arr.Length; j++) {
if (i != j) {
int sum = arr[i] + arr[j];
if (sum == res) {
Console.WriteLine(arr[i]);
}
}
}
}

## Example

using System;
using System.Collections.Generic;

namespace Demo {
public class Program {
public static void Main(string[] args) {
int[] arr = new int[] {
7,
4,
6,
2
};
// given integer
int res = 8;
Console.WriteLine("Given Integer {0}: ", res);
Console.WriteLine("Sum of:");
for (int i = 0; i < arr.Length; i++) {
for (int j = 0; j < arr.Length; j++) {
if (i != j) {
int sum = arr[i] + arr[j];
if (sum == res) {
Console.WriteLine(arr[i]);
}
}
}
}
}
}
}
Updated on 22-Jun-2020 09:21:51