# C Program To Swap Two Numbers (7 Different Ways)

In this article, we are going to write a c program to swap two numbers

We will make this program in the following way -:

1. C Program To Swap Two Numbers Using Third Variable
2. C Program To Swap Two Numbers Without Using Third Variable
3. C Program To Swap Two Numbers Using Function
4. C Program To Swap Two Numbers Using Pointer
5. C Program To Swap Two Numbers Using Call by Reference
6. C Program To Swap Two Numbers Using Call by Value
7. C Program To Swap Two Numbers Using XOR Operator

To make this program, we will use the following concept given below.

If you do not know about these topics well, then you may not understand this program, so you should know about them in detail from the link given above.

Now without wasting time let’s start programming.

## C Program To Swap Two Numbers Using Third Variable

Algorithm

• Program Start
• Declare Variable
• Input Number
• Assign Numbers into Variables
• Print Values before Swapping
• Swapping Two Numbers Using Third Variable
• Display Values after Swapping
• Program End

Program

``````//C Program To Swap Two Numbers Using Third Variable

#include <stdio.h>
int main()
{
int v1, v2, temp;
printf("Enter two Number");
scanf("%d%d", &v1, &v2);
printf("Before Swapping \n First variable = %d \n Second variable = %d \n", v1, v2);
temp = v1;
v1 = v2;
v2 = temp;
printf("After Swapping \n First variable = %d \n Second variable = %d \n", v1, v2);
return 0;
}``````

Output

``````Enter Two Number
10
20
Before Swapping
First Variable = 10
Second Variable = 20
After Swapping
First VBariable = 20
Second Variable = 10``````

## C Program To Swap Two Numbers Without Using Third Variable

Program

``````//C Program To Swap Two Numbers Without Using Third Variable

#include <stdio.h>
int main()
{
int v1, v2;
printf("Enter two Number : ");
scanf("%d%d", &v1, &v2);
printf("Before Swapping \n First variable = %d \n Second variable = %d \n", v1, v2);
v1 = v1 + v2;
v2 = v1 - v2;
v1 = v1 - v2;
printf("After Swapping \n First variable = %d \n Second variable = %d \n", v1, v2);
return 0;
}``````

Output

``````Enter two Number : 20 50
Before Swapping
First variable = 20
Second variable = 50
After Swapping
First variable = 50
Second variable = 20``````

## C Program To Swap Two Numbers Using Function

Algorithm

• Program Start
• Declare Variable
• Input Number
• Assign Numbers into Variables
• Calling Function To swap to numbers
• Print Values before Swapping
• Swapping Two Numbers Using Third Variable
• Display Values after Swapping
• Program End

Program

``````//C Program To Swap Two Numbers Using Function

#include <stdio.h>
void swap(int v1, int v2)
{
printf("Before Swapping \n First variable = %d \n Second variable = %d \n", v1, v2);
v1 = v1 + v2;
v2 = v1 - v2;
v1 = v1 - v2;
printf("After Swapping \n First variable = %d \n Second variable = %d \n", v1, v2);
}
int main()
{
int n1, n2;
printf("Enter two Number : ");
scanf("%d%d", &n1, &n2);
swap(n1,n2);
return 0;
}
``````

Output

``````Enter two Number : 10  30
Before Swapping
First variable = 10
Second variable = 30
After Swapping
First variable = 30
Second variable = 10``````

## C Program To Swap Two Numbers Using Pointer

Program

``````#include<stdio.h>

void swap(int *x, int *y);
int main()
{
int a,b;
printf("Enter Two Numbers : ");
scanf("%d %d",&a,&b);
printf("Before Swapping a = %d and b = %d \n",a,b);
swap(&a,&b);

printf("After Swapping a = %d and b = %d ",a,b);

return 0;
}
void swap(int *x,int *y)  //Here x and y is a pointer variable
{
int t;
t=*x;
*x=*y;
*y=t;
}``````

Output

``````Enter Two Numbers : 50  100
Before Swapping a = 50 and b = 100
After Swapping a = 100 and b = 50``````

## C Program To Swap Two Numbers Using Call by Reference

Program

``````#include<stdio.h>

void swap(int *x, int *y);
int main()
{
int a = 100, b = 200;
printf("Before Swapping a= %d and b= %d \n",a,b);
swap(&a,&b);

printf("After Swapping a= %d and b= %d ",a,b);

return 0;
}
void swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}``````

Output

``````Before Swapping a= 100 and b= 200
After Swapping a= 200 and b= 100``````

## C Program To Swap Two Numbers Using Call by Value

Program

``````//C Program To Swap Two Numbers Using Call by Value

#include<stdio.h>
void swap(int x,int y);
int main()
{
int a=10,b=20;

printf("Before Swapping a= %d and b= %d \n",a,b);
swap(a,b);

return 0;
}
void swap(int x,int y)
{ int t;
t=x;
x=y;
y=t;
printf("After Swapping a= %d and b= %d ",x,y);
}``````

Output

``````Before Swapping a= 10 and b= 20
After Swapping a= 20 and b= 10``````

## C Program To Swap Two Numbers Using XOR Operator

Program

``````//C Program To Swap Two Numbers Without Using XOR Operator
#include <stdio.h>
int main()
{
int v1, v2;
printf("Enter two Number : ");
scanf("%d%d", &v1, &v2);
printf("Before Swapping \n First variable = %d \n Second variable = %d \n", v1, v2);
v1 = v1 ^ v2;
v2 = v1 ^ v2;
v1 = v1 ^ v2;
printf("After Swapping \n First variable = %d \n Second variable = %d \n", v1, v2);
return 0;
}``````

Output

``````Enter two Number : 40  60
Before Swapping
First variable = 40
Second variable = 60
After Swapping
First variable = 60
Second variable = 40``````

## Conclusion

So friends, in this article, we have written a C Program To Swap Two Numbers

1. C Program To Swap Two Numbers Using Third Variable
2. C Program To Swap Two Numbers Without Using Third Variable
3. C Program To Swap Two Numbers Using Function
4. C Program To Swap Two Numbers Using Pointer
5. C Program To Swap Two Numbers Using Call by Reference
6. C Program To Swap Two Numbers Using Call by Value
7. C Program To Swap Two Numbers Using XOR Operator

If you have difficulty to understand any program or have any doubts or questions, then tell me in the comment below.

Jeetu Sahu is A Web Developer | Computer Engineer | Passionate about Coding, Competitive Programming, and Blogging