Computer scienceProgramming languagesC++Operators and expressions

Operator precedence and associativity rules

Swap numbers

Report a typo

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.

Sample Input 1:

3 4

Sample Output 1:

Before swapping: num1 = 3, num2 = 4
After swapping: num1 = 4, num2 = 3

Sample Input 2:

6 9

Sample Output 2:

Before swapping: num1 = 6, num2 = 9
After swapping: num1 = 9, num2 = 6
Write 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