added swapping of two integers in c++ #994
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This C++ program is designed to swap the values of two user-input integers using a dedicated swap function. Here’s a detailed breakdown of the program:
Program Structure:
Headers and Namespace:
The program includes the iostream header to handle input/output operations and uses the std namespace to avoid prefixing std:: before standard library functions like cin and cout. Swap Function:
The swap function is defined to take two integer references (int &a and int &b) as parameters. By passing by reference, the original values of a and b are swapped inside the function. The function works by: Storing the value of a in a temporary variable temp. Assigning the value of b to a.
Assigning the value of temp (original a) to b.
This effectively swaps the values of the two integers without returning anything. Main Function:
The main function starts by declaring two integers, a and b, to hold the numbers input by the user. It prompts the user to input the first and second numbers, storing them in a and b respectively. The values of a and b are displayed before the swap operation. The swap function is then called to interchange the values of a and b. After the swap operation, the updated values of a and b are printed to show the successful swapping of numbers.