One-liner Python Approach for Swapping two numbers.
Difficulty Level: Very Easy
Answers
Answer:
import java.util.*;
public class SwapNumber {
public static void main(String args[]) {
int original_number,reversed_number="";
original_number=(new Scanner(System.in)).nextInt();
for(int i=original_number.length()-1;i>=0;i--)
reversed_number+=original_number.charAt(i);
System.out.println(reversed_number);
}
}
If swap two numbers means reverse them , then it might be correct.
But I did not run IT.
So it is incorrect. :-((
Solution:
Here comes the códe -
a=int(input("Enter first number: "))
b=int(input("Enter second number: "))
print("Before Swapping,")
print(f"a = {a}\nb = {b}")
a,b=b,a
print("After Swapping,")
print(f"a = {a}\nb = {b}")
The swapping logic is very easy. Here, swapping takes place in the fifth line. In other languages, we have to use temporary variable but in python, we can easily swap two numbers without using third variable.
See the attachment for output.
•••♪