Introduction: In this article i will explain How to swap/interchange the values of two numbers using C language with an example program.
Description: Swapping the value of two numbers in C Language is off course a very easy 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 Addition (+) and Subtraction (-) arithmetic operators to help in swapping. In previous articles How to swap two numbers in C Language ? I explained how to swap two numbers using temporary variable and in another article Another way to swap two numbers without using temporary variable in C Language? i explained one more way to swap two numbers in C Language .
Implementation: let's create a program to swap the value of two numbers.
Description: Swapping the value of two numbers in C Language is off course a very easy 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 Addition (+) and Subtraction (-) arithmetic operators to help in swapping. In previous articles How to swap two numbers in C Language ? I explained how to swap two numbers using temporary variable and in another article Another way to swap two numbers without using temporary variable in C Language? i explained one more way to swap two numbers in C Language .
Implementation: let's create a program to swap the value 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."
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..