ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   SiteMap shows list of All Categories in this site.
Google
 
Categories  >>  Code Snippets  >>  Programming Code  >>  C Code
 
 


 

 
 C Code interview questions  C Code Interview Questions
 C++ Code interview questions  C++ Code Interview Questions
 VC++ Code interview questions  VC++ Code Interview Questions
 Java Code interview questions  Java Code Interview Questions
 Dot Net Code interview questions  Dot Net Code Interview Questions
 Visual Basic Code interview questions  Visual Basic Code Interview Questions
 Programming Code AllOther interview questions  Programming Code AllOther Interview Questions
Question
How to swap two variables, without using third variable ?
 Question Submitted By :: Swapna
I also faced this Question!!     Rank Answer Posted By  
 
  Re: How to swap two variables, without using third variable ?
Answer
# 1
Hi this question was asked in my interview. 
Ans is :

a=a+b;
b=a-b;
a=a-b;

Any other solution?
 
Is This Answer Correct ?    246 Yes 26 No
Swapna
 
  Re: How to swap two variables, without using third variable ?
Answer
# 2
use xor to swap

a = a^b
b= a^b
a= a^b
 
Is This Answer Correct ?    93 Yes 31 No
Guest
 
 
 
  Re: How to swap two variables, without using third variable ?
Answer
# 3
Ans is :

a=a*b;
b=a/b;
a=a/b;
 
Is This Answer Correct ?    65 Yes 40 No
Somasekhar
 
  Re: How to swap two variables, without using third variable ?
Answer
# 4
a=5,b=10
a=a-b;
b=a+b;
a=a+b;
 
Is This Answer Correct ?    31 Yes 57 No
Vinay_csjm
 
  Re: How to swap two variables, without using third variable ?
Answer
# 5
Only #2 will work.  The others may over/under flow.
 
Is This Answer Correct ?    15 Yes 20 No
Er
 
  Re: How to swap two variables, without using third variable ?
Answer
# 6
a=a+b;
b=a-b;
a=a-b
 
Is This Answer Correct ?    48 Yes 10 No
Kiran
 
  Re: How to swap two variables, without using third variable ?
Answer
# 7
b=a-b;
a=a-b;
b=a+b;
 
Is This Answer Correct ?    12 Yes 16 No
P.muthukumar
 
  Re: How to swap two variables, without using third variable ?
Answer
# 8
2 one will work correct
 
Is This Answer Correct ?    21 Yes 5 No
Gowtham
 
  Re: How to swap two variables, without using third variable ?
Answer
# 9
plz check 3 aswer is perfect, 2 one mat be wrong in some cases
 
Is This Answer Correct ?    12 Yes 13 No
Koneru Gowtham
 
  Re: How to swap two variables, without using third variable ?
Answer
# 10
a=b-a+(b=a);
 
Is This Answer Correct ?    14 Yes 20 No
Partha
 
  Re: How to swap two variables, without using third variable ?
Answer
# 11
Only the Xor answer (#2) is correct (in cases where the 
variables are the same size).  With all the other answers, 
you could run into over/under flow problems.
A = 01111111
B = 01111101
             
A = A^B = 00000010  
B = A^B = 01111111 (Original A)
A = A^B = 01111101 (Original B)
 
Is This Answer Correct ?    30 Yes 7 No
Bruce Tuskey
 
  Re: How to swap two variables, without using third variable ?
Answer
# 12
a=a*b/(b=a);
 
Is This Answer Correct ?    4 Yes 24 No
Divakar
 
  Re: How to swap two variables, without using third variable ?
Answer
# 13
Answer no 12 is wrong. the logic fails when a=0;
 
Is This Answer Correct ?    14 Yes 6 No
Yash
 
  Re: How to swap two variables, without using third variable ?
Answer
# 14
x = x + y; 

    y = x - y; 

    x = x - y;
 
Is This Answer Correct ?    26 Yes 5 No
Mohit
 
  Re: How to swap two variables, without using third variable ?
Answer
# 15
NOne of the answer is correct except the 2ND one.....Please 
don't give wrong answers.
 
Is This Answer Correct ?    14 Yes 5 No
Shubham
 
  Re: How to swap two variables, without using third variable ?
Answer
# 16
x = y | (y=x);
 
Is This Answer Correct ?    3 Yes 18 No
Javaskills
 
  Re: How to swap two variables, without using third variable ?
Answer
# 17
Answer #16 is totally wrong it fails 
when x>y and many other situation.
 
Is This Answer Correct ?    9 Yes 3 No
3uggy3oy
 
  Re: How to swap two variables, without using third variable ?
Answer
# 18
a=(a>b?a:b)
 
Is This Answer Correct ?    3 Yes 18 No
Krishna
 
  Re: How to swap two variables, without using third variable ?
Answer
# 19
using ref keyword we can swap 2 number's

emaple
class swap
{
public void add(ref int a,ref int b)
{
int z;
z=a;
a=b;
b=z;
}
}
class swapdemo
{
static void main()
{
int x=10,y=5;
ankush obj=new ankush();
Console.writeline{"befor value's"+a+","+b};
obj.swap(ref x, ref y);
Console.writeline("after values"+a+","+b);
}
}
 
Is This Answer Correct ?    6 Yes 23 No
Ankush
 
  Re: How to swap two variables, without using third variable ?
Answer
# 20
Using Assembly language( Using Accumulator)...Without using 
any arithmatic...without using any Pointer...without 
declaring third varible

int a = 20;
int b = 10;
   __asm
  {
	  mov EAX,b
	  push EAX
	  mov EAX,a
	  mov b,EAX
	  pop EAX
	  mov a,EAX
  }
 
Is This Answer Correct ?    5 Yes 13 No
Kumar
 
  Re: How to swap two variables, without using third variable ?
Answer
# 21
The first two answers are correct. Third will FAIL in the 
case the second num is 0. The #19 one still uses a third 
variable.
 
Is This Answer Correct ?    0 Yes 10 No
Meenama
 
  Re: How to swap two variables, without using third variable ?
Answer
# 22
using X-OR 
#define SWAP(x,y) x^=y^=x^=y

x = x ^ y --> x^=y -- (1)
y = y ^ x --> y^=x -- (2)
x = x ^ y --> x^=y -- (3)

(3) in (2) --> y^=x^=y -- (4)
(4) in (1) --> x^=y^=x^=y -- :-)

all togeather, he single line code

#define SWAP(x,y) x^=y^=x^=y
 
Is This Answer Correct ?    8 Yes 6 No
Trinath Somarouthu
 
  Re: How to swap two variables, without using third variable ?
Answer
# 23
Thanks a lot guys. It is giving very good analysis.
 
Is This Answer Correct ?    2 Yes 5 No
Saumya
 
  Re: How to swap two variables, without using third variable ?
Answer
# 24
i am not giving answer
all above answer for swaping integer type variables without 
third
i want answer in string type
 
Is This Answer Correct ?    1 Yes 8 No
Vinay Kumar Shukla
 
  Re: How to swap two variables, without using third variable ?
Answer
# 25
a=a+b-(b=a);
 
Is This Answer Correct ?    9 Yes 10 No
Hanmanth Reddy
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
main() { char not; not=!2; printf("%d",not); }  1
main() { extern int i; i=20; printf("%d",sizeof(i)); }  1
write a program in c to merge two array  1
void main() { void *v; int integer=2; int *i=&integer; v=i; printf("%d",(int*)*v); }  1
main() { int i =10, j = 20; clrscr(); printf("%d, %d, ", j-- , --i); printf("%d, %d ", j++ , ++i); } a. 20, 10, 20, 10 b. 20, 9, 20, 10 c. 20, 9, 19, 10 d. 19, 9, 20, 10 HCL1
#ifdef something int some=0; #endif main() { int thing = 0; printf("%d %d\n", some ,thing); }  1
#include<stdio.h> main() { FILE *ptr; char i; ptr=fopen("zzz.c","r"); while((i=fgetch(ptr))!=EOF) printf("%c",i); }  1
Write a prog to accept a given string in any order and flash error if any of the character is different. For example : If abc is the input then abc, bca, cba, cab bac are acceptable, but aac or bcd are unacceptable. Microsoft5
how to check whether a linked list is circular.  3
Write a routine that prints out a 2-D array in spiral order Microsoft2
main() { int i=5; printf(“%d”,i=++i ==6); }  1
program to find magic aquare using array HCL3
How we will connect multiple client ? (without using fork,thread) TelDNA2
Finding a number which was log of base 2 NetApp1
void main() { int k=ret(sizeof(float)); printf("\n here value is %d",++k); } int ret(int ret) { ret += 2.5; return(ret); }  1
plz send me all data structure related programs  2
How will you print % character? a. printf(“\%”) b. printf(“\\%”) c. printf(“%%”) d. printf(“\%%”) HCL1
main() { char a[4]="HELL"; printf("%s",a); } Wipro1
main() { int i; float *pf; pf = (float *)&i; *pf = 100.00; printf("\n %d", i); } a. Runtime error. b. 100 c. Some Integer not 100 d. None of the above HCL1
1. const char *a; 2. char* const a; 3. char const *a; -Differentiate the above declarations.  2
 
For more C Code Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com