Search
 
SCRIPT & CODE EXAMPLE
 

CPP

C++ Swap 2 Variables Without Using 3rd Variable

	int num1, num2, temp;
	cout << " Input 1st number : ";
	cin >> num1 ;
	cout << " Input 2nd number : ";
	cin >> num2;	
	num2=num2+num1;
	num1=num2-num1;
	num2=num2-num1;
    cout << " After swapping the 1st number is : "<< num1 <<"
" ; 
    cout << " After swapping the 2nd number is : "<< num2 <<"

" ; 	
Comment

swapping without third variable c++

//Author: Subodh
        //! Swap two number using XOR operation
        cout << "Before, n1 = " << num1 << ", n2 = " << num2 << endl;
        num1 = num1 ^ num2, num2 = num1 ^ num2, num1 = num1 ^ num2;
        cout << "After, n1 = " << num1 << ", n2 = " << num2 << endl;
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ terminal color 
Cpp :: delete one specific character in string C++ 
Cpp :: footnote appears in the middle latex 
Cpp :: const char to string 
Cpp :: opencv c++ image write 
Cpp :: string to long integer c++ 
Cpp :: remove last index of the string in c++ 
Cpp :: for loop in c++ 
Cpp :: c++ cout colored output xcode 
Cpp :: how to store pair in min heap in c++ 
Cpp :: Story of c++ 
Cpp :: remove element from array c++ 
Cpp :: change int to string c++ 
Cpp :: c++ template function 
Cpp :: Xor implementation C++ 
Cpp :: c++ remove last character from string 
Cpp :: C++ String Length Example 
Cpp :: lambda c++ 
Cpp :: remove from vector by value c++ 
Cpp :: even and odd sum in c++ 
Cpp :: int to float c++ 
Cpp :: modulo subtraction 
Cpp :: how to square a number in c++ 
Cpp :: how to add c++14 in sublime text 
Cpp :: how to empty string c++ 
Cpp :: factorial calculator c++ 
Cpp :: how can we create 4 digit random number in c++ 
Cpp :: C++ Infinite while loop 
Cpp :: cin exceptions c++ 
Cpp :: 31. Next Permutation leetcode solution in c++ 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =