Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Showing posts with label Swap two variables without using third variable.. Show all posts
Showing posts with label Swap two variables without using third variable.. Show all posts

Monday, 25 March 2019

The Interview Question Today#26(25-3-19) (Swap two variables without using third variable.)

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