def swap_numbers(a, b):
a = a + b
b = a - b
a = a - b
return a, b
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
a, b = swap_numbers(a, b)
print(f"After swapping: First number = {a} and Second number = {b}")
#source code --> clcoding.com
Code Explanation:
Function: swap_numbers
def swap_numbers(a, b):
a = a + b
b = a - b
a = a - b
return a, b
Purpose: This function swaps the values of a and b using arithmetic operations.
Process:
a = a + b: The sum of a and b is stored in a.
b = a - b: The new value of b becomes the original value of a.
a = a - b: The new value of a becomes the original value of b.
Return Value: The function returns the swapped values of a and b.
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
Input:
Prompts the user to enter two integers (a and b).
Converts the inputs into integers using int().
a, b = swap_numbers(a, b)
Function Call: Passes the user-provided values of a and b to the swap_numbers function.
Result: After the function executes, the values of a and b are swapped.
print(f"After swapping: First number = {a} and Second number = {b}")
Output:
Prints the swapped values of a and b.
0 Comments:
Post a Comment