Advertisement
Spocoman

04. Sum of Two Numbers

Sep 11th, 2023
940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.  
  7.     int startNum, endNum, magicNum;
  8.     cin >> startNum >> endNum >> magicNum;
  9.  
  10.     bool isMagic = false;
  11.  
  12.     int counter = 0;
  13.  
  14.     for (int i = startNum; i <= endNum; i++) {
  15.         for (int j = startNum; j <= endNum; j++) {
  16.             counter++;
  17.             if (i + j == magicNum) {
  18.                 cout << "Combination N:" << counter << " (" << i << " + " << j << " = " << magicNum << ")" << endl;
  19.                 isMagic = true;
  20.                 break;
  21.             }
  22.         }
  23.         if (isMagic == true) {
  24.             break;
  25.         }
  26.     }
  27.  
  28.     if (isMagic == false) {
  29.         cout << counter << " combinations - neither equals " << magicNum << endl;
  30.     }
  31.  
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement