How to swap two numbers in C Language ?

Introduction: How to swap/Interchange the value of two numbers/values in C Language is very basic task. There are a number of ways to swap the value of two numbers in C Language.  In this article I am going to use a temporary variable or we can say third variable to help in swapping. In the article How to swap two numbers without using temporary variable in C Language?  I have explained how to swap two numbers without using temporary variable and there is also my another article Another way to swap two numbers without using temporary variable in C Language?  that explains another way to swap two numbers in C Language.

Program example: Let's understand by an program

#include<stdio.h>
#include<conio.h>
void main()
{
 int a,b,temp;
  clrscr();
 printf("Enter the value of a= ");
 scanf("%d",&a);
 printf("\nEnter the value of b= ");
 scanf("%d",&b);
 printf("\nValue of a before swapping= %d",a);
 printf("\nValue of b before swapping= %d",b);
 temp=a;
 a=b;
 b=temp;
 printf("\nValue of a after swapping= %d",a);
 printf("\nValue of b after swapping= %d",b);
 getch();
 }

Run the program using ctrl+F9

Output:
Enter the value of a=10
Enter the value of b=20

Value of a before swapping=10
Value of b before swapping=20
Value of a after swapping=20
Value of b after swapping=10

Now over to you:

"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

1 comments:

Click here for comments
Anonymous
admin
April 05, 2013 × This comment has been removed by a blog administrator.
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..