Hacktoberfest2021 icon indicating copy to clipboard operation
Hacktoberfest2021 copied to clipboard

added swapping of two integers in c++

Open lavya30 opened this issue 4 months ago • 0 comments

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.

lavya30 avatar Oct 17 '24 10:10 lavya30