employer cover photo
employer logo
employer logo

People Tech Group

Is this your company?

People Tech Group Interview Question

write a program to swap two numbers in 3 different ways

Interview Answer

Anonymous

May 8, 2018

#include int main() { int a=10,b=15; int c; /*1. By using one temporary variable*/ c=b; b=a; a=c; /*2. without using extra variable*/ a=a+b; b=a-b; /* here a=a+b; so we get b=a+b-b; */ a=a-b; /* here previous value of a=a+b; so we get a=a+b-b(which is equal to a); /*3. by using pointers */ int *x,*y,*z; x=&a; y=&b; z=y; y=x; x=z; return 0; }