Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Saturday, February 23, 2019

C Program for Swapping without third variable by G Krishna Chauhan

#include<stdio.h>  
#include<conio.h>  
main()  
{  
int a=10, b=20;    
clrscr();    
printf("Before swap a=%d b=%d",a,b);    
a=a+b;//a=30 (10+20)  
b=a-b;//b=10 (30-20)  
a=a-b;//a=20 (30-10)  
printf("\nAfter swap a=%d b=%d",a,b);  
getch();  
}  

...................................................................................


#include<stdio.h>  
#include<conio.h>  
main()  
{  
int a=10, b=20;    
clrscr();    
printf("Before swap a=%d b=%d",a,b);    
a=a*b;//a=200 (10*20)  
b=a/b;//b=10 (200/20)  
a=a/b;//a=20 (200/10)  
printf("\nAfter swap a=%d b=%d",a,b);   
getch();  

}  

No comments:

Post a Comment