C Program to Swap Two Numbers without using Third Variable
C Program to Swap Two Numbers using Temporary Variables:
#include<stdio.h> #include<conio.h> void main() { int x,y, temp; clrscr(); printf("Enter the value of x :"); scanf("%d",&x); printf("Enter the value of y :"); scanf("%d",&y); temp=x; x=y; y=temp; printf("\nThe value of x =%d",x); printf("\nThe value of y =%d",y); getch(); }
Output :
Enter the value of x :25
Enter the value of y :30
The value of x =30
The value of y =25
C Program to Swap Two Numbers Without using Temporary Variables :
#include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("Enter the value of a :"); scanf("%d",&a); printf("Enter the value of b :"); scanf("%d",&b); a=a+b; b=a-b; a=a-b; printf("After swapping: a=%d,b=%d",a,b); getch(); }
Output :
Enter the value of a :15
Enter the value of b :10
After swapping: a=10,b=15