Question
Swap two variables without using third variable.
Example:
Input: A=5, B=10
Output: A=10 B=5
Solution:
C program:
#include<stdio.h>
int main(){
int a=5,b=10;
printf("\nBefore Swap: a= %d b= %d",a,b);
a=a^b;
b=a^b;
a=b^a;
printf("\nAfter Swap: a= %d b= %d",a,b);
}
Output:
Before Swap: a= 5 b= 10
After Swap: a= 10 b= 5
0 comments:
Post a Comment