Advertisement
Spocoman

09. Sum of Two Numbers

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