How to swap 2 number in programming?

How to swap 2 numbers?

There is two number in two variables. We have to swap the position of the numbers. For example, the

swap 2 number in programming
Swap

variable x holding integer value 5 and another variable y holding the integer value 10. Now we have to swap the position of those two numbers.

Logic: 

We can use another temporary variable to store the value of x.

swap 2 number in programming
Swap fig: 01

Now we have a backup copy of the value of x into the temp variable.
So, we can now give the value of y to x.

swap 2 number in programming
Swap fig: 02

Yes, we have done half of our job!! x is now holding the value of y. Now time to assign the value of x into y. Remember, we have saved the value of x into the temp variable in figure 01. So now put the temp value to y.

swap 2 number in programming
Swap fig: 03

Oh yes. Now look...! We have successfully swapped the value of x to y and the importance of y to x.

Now it's time to check what we understand. Write a program to swap two numbers:

[Note: Don't see the code first. Please try yourself] alert-info

Sample Input:

swap 2 number in programming
Sample Input

The program should take input from the user.

Sample Output:

swap 2 number in programming
Sample Output

Don't worry. If you failed, the code is here. Hope you will understand the code easily.

-----Happy Coding-----

8/Post a Comment/Comments

  1. Your guide is very simple to understand thanks for sharing...
    Regards,
    PHP Training in Chennai|PHP Course in Chennai

    ReplyDelete
  2. These examples are really helpful to follow the instructions, Thanks for taking your precious time to explain about this concept.
    Regards,
    web designing training|web designing training in chennai|web design training in chennai

    ReplyDelete
  3. I like it your blog. Thank you for Sharing the valuable information!
    erp in chennai | erp providers in chennai

    ReplyDelete
  4. thank you for such a great article with us. hope it will be much useful for us. please keep on updating..
    Web D School

    ReplyDelete
  5. Thanks for sharing an good informative blog with more details.I like the helpful info you provide in your articles. I have saved it for my future reference. Keep it up the good work.
    Sundaram Home Finance Limited
    Home Loans
    Plot Loans
    Home Improvement Loans
    Home Extension Loans
    Top Up Loans
    Take Over Loan
    Mortgage Loan

    ReplyDelete
  6. Thanks for sharing an good informative blog with more details.I like the helpful info you provide in your articles. I have saved it for my future reference. Keep it up the good work.
    Sundaram Home Finance Limited
    Home Loans
    Plot Loans
    Home Improvement Loans
    Home Extension Loans
    Top Up Loans
    Take Over Loan
    Mortgage Loan

    ReplyDelete

Post a Comment