Another way to swap two numbers without using temporary variable in C Language?

Introduction: In this article i am going to explain with example program How to swap/interchange the value of two numbers without using third variable in C language.

Description: Swapping the value of two numbers in C Language is off course not a difficult task. But how many of us actually know that there are multiple ways to swap the value of two variables in C language. There are a number of ways to swap the value of two numbers in C Language.  In this article I am going to use Multiplication (*) and Division (/) arithmetic operators to help in swapping. In previous article How to swap two numbers in C Language ?  I explained how to swap two numbers using temporary variable and there is also my another article How to swap two numbers without using temporary variable in C Language?  that explains another way to swap two numbers in C Language .

Implementation: let's understand by creating a program to swap the values of two numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
 int a,b;
  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);
 a=a*b;
 b=a/b;
 a=a/b;
 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..