SWAP NUMBERS IN C++

Swap Numbers (Using Temporary Variable)

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int a = 5, b = 10, temp;
  6. cout << "Before swapping." << endl;
  7. cout << "a = " << a << ", b = " << b << endl;
  8. temp = a;
  9. a = b;
  10. b = temp;
  11. cout << "\nAfter swapping." << endl;
  12. cout << "a = " << a << ", b = " << b << endl;
  13. return 0;
  14. }
Output
Before swapping.
a = 5, b = 10

After swapping.
a = 10, b = 5

Comments

Post a Comment

Popular posts from this blog

Scan Converting a sStraight Line

priority&non priority scheduling

DDA and Bresenham's Algorithm