52.6k views
0 votes
Write a program that will read two floating point numbers (the first read into a variable called first and the second read into a variable called second) and then calls the function swap with the actual parameters first and second. The swap function having formal parameters number1 and number2 should swap the value of the two variables.

Sample Run:
Enter the first number
Then hit enter
80
Enter the second number
Then hit enter
70
You input the numbers as 80 and 70.
After swapping, the first number has the value of 70 which was the value of the
second number
The second number has the value of 80 which was the value of the first number

User Quy Tang
by
4.8k points

1 Answer

3 votes

Answer:

The program in Python is as follows:

def swap(number1,number2):

number1,number2 = number2,number1

return number1, number2

first = int(input("First: "))

second = int(input("Second: "))

print("You input the numbers as ",first,"and",second)

first,second= swap(first,second)

print("After swapping\\First: ",first,"\\Second:",second)

Step-by-step explanation:

This defines the function

def swap(number1,number2):

This swaps the numbers

number1,number2 = number2,number1

This returns the swapped numbers

return number1, number2

The main begins here

This gets input for first

first = int(input("First: "))

This gets input for second

second = int(input("Second: "))

Print the number before swapping

print("You input the numbers as ",first,"and",second)

This swaps the numbers

first,second= swap(first,second)

This prints the numbers after swapping

print("After swapping\\First: ",first,"\\Second:",second)

User Zooko
by
4.8k points