Answer:
Here is the C++ and Python programs:
C++ program:
#include<iostream> //to use input output functions
using namespace std; //to identify objects like cin cout
void swapvalues(int& userVal1, int& userVal2);
//function to swap values
int main() { //start of main function
int integer1,integer2; //declare two integers
cout<<"Enter first integer: "; //prompts user to enter the first number
cin>>integer1; //reads first number from user
cout<<"Enter second integer: ";
//prompts user to enter the second number
cin>>integer2; //reads second number from user
cout<<"Integers before swapping:\n"; //displays two numbers before swapping
cout<<integer1<<endl<<integer2;
swapvalues(integer1, integer2); //calls swapvalues method passing two integers to it
cout << "\nIntegers after swapping:\n";
//displays two numbers after swapping
cout<<integer1<<endl<<integer2; }
void swapvalues(int& userVal1, int& userVal2){ //method that takes two int type numbers and swaps these numbers
int temp; //temporary variable
temp = userVal1; //temp holds the original value of userVal1
userVal1 = userVal2; //the value in userVal2 is assigned to userVal1
userVal2 = temp; } //assigns value of temp(original value of userVal1) to userVal2
The program prompts the user to enter two integers and then calls swapvalues() method by passing the integers by reference. The swapvalues() method modifies the passed parameters by swapping them. The method uses a temp variable to hold the original value of first integer variable and then assigns the value of second integer variable to first. For example if userVal1 = 1 and userVal2 =2. Then these swapping statements work as follows:
temp = userVal1;
temp = 1
userVal1 = userVal2;
userVal1 = 2
userVal2 = temp;
userVal2 = 1
Now because of these statements the values of userVal1 and userVal2 are swapped as:
userVal1 = 2
userVal2 = 1
Explanation:
Python program:
def swap_values(user_val1, user_val2): #method to swap two numbers
return user_val2,user_val1
#returns the swapped numbers
integer1 = int(input('Enter first integer :'))
#prompts user to enter first number
integer2 = int(input('Enter second integer :')) #prompts user to enter second number
print("Numbers before swapping")
#displays numbers before swapping
print(integer1,integer2)
print("Numbers after swapping")
#displays numbers after swapping
integer1,integer2 = swap_values(integer1,integer2)
#calls method passing integer1 and integer2 to swap their values
print(integer1,integer2)
#displays the swapped numbers
The screenshot of programs and outputs is attached.