Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added swapping of two integers in c++ #994

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lavya30
Copy link

@lavya30 lavya30 commented Oct 17, 2024

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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant