fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int arr[] = {6, 8, 4, -5, 7, 9};
  5. int size = sizeof(arr) / sizeof(arr[0]);
  6. int sum = 15;
  7. int found = 0;
  8.  
  9. for (int i = 0; i < size - 1; i++) {
  10. for (int j = i + 1; j < size; j++) {
  11. if (arr[i] + arr[j] == sum) {
  12. printf("Pair of elements can make the given sum by the value of index %d and %d (%d + %d = %d)\n", i, j, arr[i], arr[j], sum);
  13. found = 1;
  14. break; // Remove break if you want to find all pairs
  15. }
  16. }
  17. if (found) break;
  18. }
  19.  
  20. if (!found) {
  21. printf("No pair found with the given sum.\n");
  22. }
  23.  
  24. return 0;
  25. }
  26.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Pair of elements can make the given sum by the value of index 0 and 5 (6 + 9 = 15)