ALLInterview.com :: Home Page            
 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                      
Google
   
 
Categories >> Code Snippets >> Programming Code >> C Code
 
 


 

 
 C Code interview questions  C Code Interview Questions (401)
 C++ Code interview questions  C++ Code Interview Questions (121)
 VC++ Code interview questions  VC++ Code Interview Questions (2)
 Java Code interview questions  Java Code Interview Questions (57)
 Dot Net Code interview questions  Dot Net Code Interview Questions (113)
 Visual Basic Code interview questions  Visual Basic Code Interview Questions (8)
 Programming Code AllOther interview questions  Programming Code AllOther Interview Questions (37)
Question
How to swap two variables, without using third variable ?
 Question Submitted By :: C-Code
I also faced this Question!!     Answer Posted By  
 
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 ?    1713 Yes 218 No
Swapna
 
Answer
# 2
use xor to swap

a = a^b
b= a^b
a= a^b
 
Is This Answer Correct ?    532 Yes 161 No
Guest
 
 
 
Answer
# 3
Ans is :

a=a*b;
b=a/b;
a=a/b;
 
Is This Answer Correct ?    525 Yes 178 No
Somasekhar
 
Answer
# 4
a=a+b;
b=a-b;
a=a-b
 
Is This Answer Correct ?    292 Yes 58 No
Kiran
 
Answer
# 5
x = x + y;

y = x - y;

x = x - y;
 
Is This Answer Correct ?    104 Yes 23 No
Mohit
 
Answer
# 6
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 ?    87 Yes 21 No
Bruce Tuskey
 
Answer
# 7
a=b-a+(b=a); 
Is This Answer Correct ?    113 Yes 76 No
Partha
 
Answer
# 8
2 one will work correct 
Is This Answer Correct ?    80 Yes 44 No
Gowtham
 
Answer
# 9
void main()
{
int a,b;
printf("Enter two number : ");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("The swap number is : %d %d",a,b);
getch();
}
 
Is This Answer Correct ?    39 Yes 9 No
Pavan.
 
Answer
# 10
Answer no 12 is wrong. the logic fails when a=0; 
Is This Answer Correct ?    41 Yes 17 No
Yash
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
void ( * abc( int, void ( *def) () ) ) ();   1
int main() { int x=10; printf("x=%d, count of earlier print=%d", x,printf("x=%d, y=%d",x,--x)); getch(); } ================================================== returns error>> ld returned 1 exit status =================================================== Does it have something to do with printf() inside another printf().   2
main() { extern int i; { int i=20; { const volatile unsigned i=30; printf("%d",i); } printf("%d",i); } printf("%d",i); } int i;   1
how to print 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 using any loop(for or while) only once(only 1 loop) and maximum 2 variables using C. Cap-Gemini 16
Is this code legal? int *ptr; ptr = (int *) 0x400;   1
How do you sort a Linked List (singly connected) in O(n) please mail to pawan.10k@gmail.com if u can find an anser...i m desperate to knw... Oracle 6
main() { int i=-1; +i; printf("i = %d, +i = %d \n",i,+i); }   1
How to swap two variables, without using third variable ? HCL 104
How do I write a program to print proper subset of given string . Eg :input: abc output:{},{a},{b},{c},{a,b},{a,c},{b,c}, {a,b,c}.I desperately need this program please mail me to saravana6m@gmail.com Deshaw 11
main() { int i = 258; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); }   1
Give a very good method to count the number of ones in a 32 bit number. (caution: looping through testing each bit is not a solution) Microsoft 7
plz send me all data structure related programs   2
 
For more C Code Interview Questions Click Here 
 
 
 
 
 


   
Copyright Policy  |  Terms of Service  |  Articles  |  Site Map  |  RSS Site Map  |  Contact Us
   
Copyright 2013  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  KalAajKal.com