• Post author:
• Reading time:3 mins read
• Post category:C Programs

In this programming tutorial, you will be able to learn how to swap two numbers. There are 4 methods explained.

In order to understand the programming examples, you should have some knowledge of the following C-Programming concepts:

Swap Two Numbers Using Temporary Variable

``````#include<stdio.h>
int main(int argc, char const *argv[])
{
int number1,number2,temp;
printf("\nSwapping of two numbers in c\nEnter two numbers \n");
scanf("%d%d",&number1,&number2);
printf("Number 1: %d\nNumber 2: %d",number1,number2);
//swapping
temp=number1;
number1=number2;
number2=temp;
printf("\nAfter swapping");
printf("\nNumber 1: %d\nNumber 2: %d\n",number1,number2);
return 0;
}
C Program to swap two numbers``````

In the above programming example, the user is asked to enter 2 numbers. The numbers are stored in `number1` and `number2`.

By using a temporary variable numbers entered by the user are swapped. i.e.

`temp=number1;number1=number2;number2=temp;`

After swapping two numbers `number1` and `number2` are printed onto the console.

Swap Two Numbers Without Third Variable by Addition and Subtraction

``````#include<stdio.h>
int main(int argc, char const *argv[])
{
int number1,number2;
printf("\nSwapping of two numbers in c\nEnter two numbers \n");
scanf("%d%d",&number1,&number2);
printf("Number 1: %d\nNumber 2: %d",number1,number2);
//swapping
number1=number1+number2;
number2=number1-number2;
number1=number1-number2;
printf("\nAfter swapping");
printf("\nNumber 1: %d\nNumber 2: %d\n",number1,number2);
return 0;
}``````

In the above programming example, the user is asked to enter 2 numbers. The numbers are stored in `number1` and `number2`.

By using the addition and subtraction operator the numbers entered by the user are swapped. i.e.

`number1=number1+number2;number2=number1-number2;number1=number1-number2;`

After swapping two numbers `number1` and `number2` are printed onto the console.

Swap Two Numbers without Third Variable by Multiplication and Division

``````#include<stdio.h>
int main(int argc, char const *argv[])
{
int number1,number2;
printf("\nSwapping of two numbers in c\nEnter two numbers \n");
scanf("%d%d",&number1,&number2);
printf("Number 1: %d\nNumber 2: %d",number1,number2);
//swapping
number1=number1*number2;
number2=number1/number2;
number1=number1/number2;
printf("\nAfter swapping");
printf("\nNumber 1: %d\nNumber 2: %d\n",number1,number2);
return 0;
}``````

In the above programming example, the user is asked to enter 2 numbers. The numbers are stored in `number1` and `number2`.

By using a multiplication and division operator the numbers entered by the user are swapped. i.e.

`number1=number1*number2;number2=number1/number2;number1=number1/number2;`

After swapping two numbers `number1` and `number2` are printed onto the console.

Swap Two Numbers without Third Variable by Xor Operation

``````#include<stdio.h>
int main(int argc, char const *argv[])
{
int number1,number2;
printf("\nSwapping of two numbers in c\nEnter two numbers \n");
scanf("%d%d",&number1,&number2);
printf("Number 1: %d\nNumber 2: %d",number1,number2);
//swapping
number1=number1^number2;
number2=number1^number2;
number1=number1^number2;
printf("\nAfter swapping");
printf("\nNumber 1: %d\nNumber 2: %d\n",number1,number2);
return 0;
}``````

In the above programming example, the user is asked to enter 2 numbers. The numbers are stored in `number1` and `number2`.

By using a xor operator the numbers entered by the user are swapped. i.e.

`number1=number1^number2;number2=number1^number2;number1=number1^number2;`

After swapping two numbers `number1` and `number2` are printed onto the console.