# Write a C program of swapping values of three Variables without using forth variable

Write a simple program in the C programming language to swap the numbers of three variables without using the forth variable. Make a C program by taking three variables only of integer type.  The value of a will be swapped to b, and b’s value will be swapped to c and c to a. The code of the C program is as follows –

```/* Write a simple C program to swap the values of three
variables without taking the forth varaible */
#include<stdio.h>
void main()
{
int a,b,c;
printf(" Enter values of a, b and c \n");
scanf("%d %d %d",&a,&b,&c);

printf("\n a = %d",a);
printf("\n b = %d",b);
printf("\n c = %d",c);

a=a+b+c;
b=a-b-c;
c=a-b-c;
a=a-b-c;

printf("\n After swapping their values are as below -");
printf("\n a = %d",a);
printf("\n b = %d",b);
printf("\n c = %d",c);
getch();
}```

Input
a = 14
b = 45
c = 12

Processing
a = a + b + c;
a = 14 + 45 + 12 = 71;
a = 71;

b = a – b – c;
b = 71 – 45 – 12 = 14
b = 14;

c = a – b -c;
c = 71 – 14 – 12;
c = 45;

a = a – b – c;
a = 71 – 14 – 45 = 12;
a = 12;

Output
a = 12;
b = 14;
c = 45;

#### 8 thoughts on “Write a C program of swapping values of three Variables without using forth variable”

1. asar says:

I need a C progaram swepping 3 variabels using 4 variabels.

1. Rahul says:

Why do you need asar?

2. vishnu says:

asar you can do the same program by creating a temporary variable and store the valu and swap by
temp=a
a=c
c=b
b=temp

3. Asad says:

D=a;
A=b;
B=c;
C=d;

2. harsha says:

nice program

3. Navabhaarathi.A.S says:

i’m find a 4 swaping value programs:
a= a+b+c+d
b= a-b-c-d
c=a-b-c-d
d=a-b-c-d
a=a-b-c-d

4. Navabhaarathi.A.S says:

i want to pointer concept programs, and how to swaping array matrix values…..?

5. tamilselvi says:

3 variable not using the swapping c programm