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;
I need a C progaram swepping 3 variabels using 4 variabels.
Why do you need asar?
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
D=a;
A=b;
B=c;
C=d;
nice program
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
i want to pointer concept programs, and how to swaping array matrix values…..?
3 variable not using the swapping c programm