Swap two numbers without using the third variable
Take two numbers as input and swap them and print the swapped values.
Input Format:
The first line of input contains a single integer 't', representing the total number of test cases.
The second line of input contains two integers 'a' and 'b', representing the second number.
Output Format:
The first line of output prints the swapped value of 'a' and 'b'.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= 'T' <= 10^2
-10^5 <= 'a', 'b' <= 10 ^ 5
Time Limit: 1 sec
CodingNinjas
author
2y
Algorithm :
STEP 1 : START
STEP 2 : ENTER x, y
STEP 3 : PRINT x, y
STEP 4 : x = x + y
STEP 5 : y= x - y
STEP 6 : x =x - y
STEP 7 : PRINT x, y
STEP 8 : END
CodingNinjas
author
2y
Swapping using a temporary variable
Take input in two integer datatype variables, a and b. Create a temporary variable temp and initialize it equal to a. Now make a equal to b, and finally make b equal...read more
CodingNinjas
author
2y
Using arithmetic operation
We can swap values of two variables by using a third variable and instead of that, we can use the arithmetic operation to do it without an extra variable.
The idea is to store...read more
Add answer anonymously...
Top Wunderman Thompson Commerce Java Developer interview questions & answers
Popular interview questions of Java Developer
>
Wunderman Thompson Commerce Java Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app