Introduction

In this article, we will be discussing an algorithm that returns possible (no. of pairs) digit pairs that are equal to a given number (6) using Tuple class.

Prerequisite

Suppose we pass a list of numbers List<int> { 1 , 2 , 3 , 4 , 5 , 6 } with 6 as a sum, The possible pairs should be :
C#

How?

Final Code

Below are the code and output window.
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. var lst = new List<int> { 1, 2, 3, 4, 5 };
  6. int sum = 6;
  7. List<Tuple< int, int, int >> pairs = PairsOfThreeDigitSum(lst, sum);
  8. Console.WriteLine("Possible pairs of three digits whose sum are equivalent to {0} ", sum);
  9. foreach (var _pairs in pairs)
  10. {
  11. Console.WriteLine("{0} {1} {2}", _pairs.Item1, _pairs.Item2, _pairs.Item3);
  12. }
  13. Console.ReadKey(true);
  14. }
  15. public static List<Tuple<int,int,int>> airsOfThreeDigitSum(IList<int> lstInt, int sum)
  16. {
  17. List<Tuple<int,int,int>> pairs = new List<Tuple<int,int,int>>();
  18. for (int i=0;i<lstInt.Count;i++)
  19. {
  20. for (int i=0;i<lstInt.Count;i++)
  21. {
  22. for (int i=0;i<lstInt.Count;i++)
  23. {
  24. if (sum == (lstInt[i] + lstInt[j] + lstInt[k]))
  25. {
  26. pairs.Add(Tuple.Create<int,int,int>>(I , j, k);
  27. }
  28. }
  29. }
  30. }
  31. return pairs;
  32. }
  33. }

Output

C#

Summary

In this article, I discussed how we can create a method that returns Three-Digit Pairs using Tuple class in C#.
I hope you liked my article.:)