Complete the below code to swap num1 and num2 values, without using any temporary variable. Use only the + and - operators along with the concept of operator precedence and associativity rules.
Operator precedence and associativity rules
Swap numbers
Report a typo
Sample Input 1:
3 4Sample Output 1:
Before swapping: num1 = 3, num2 = 4
After swapping: num1 = 4, num2 = 3Sample Input 2:
6 9Sample Output 2:
Before swapping: num1 = 6, num2 = 9
After swapping: num1 = 9, num2 = 6Write a program in C++
#include <iostream>
int main() {
int num1, num2;
std::cin >> num1 >> num2;
std::cout << "Before swapping: num1 = " << num1 << ", num2 = " << num2 << std::endl;
// Enter your code below this line
// Enter your code above this line
std::cout << "After swapping: num1 = " << num1 << ", num2 = " << num2 << std::endl;
return 0;
}
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.