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 ?    1715 Yes 218 No
Swapna
 
Answer
# 2
use xor to swap

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

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

y = x - y;

x = x - y;
 
Is This Answer Correct ?    106 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 ?    88 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
 
Write a program that find and print how many odd numbers in a binary tree   1
# include <stdio.h> int one_d[]={1,2,3}; main() { int *ptr; ptr=one_d; ptr+=3; printf("%d",*ptr); }   1
main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); }   3
main() { int i = 257; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); }   1
code of a program in c language that ask a number and print its decremented and incremented number.. sample output: input number : 3 321123 HCL 1
String copy logic in one line. NetApp 11
func(a,b) int a,b; { return( a= (a==b) ); } main() { int process(),func(); printf("The value of process is %d !\n ",process(func,3,6)); } process(pf,val1,val2) int (*pf) (); int val1,val2; { return((*pf) (val1,val2)); } Satyam 1
#include<stdio.h> int main() { int a=3,post,pre; post= a++ * a++ * a++; a=3; pre= ++a * ++a * ++a; printf("post=%d pre=%d",post,pre); return 0; }   3
main() { char name[10],s[12]; scanf(" \"%[^\"]\"",s); } How scanf will execute?   1
Is it possible to type a name in command line without ant quotes? Infosys 1
how to check whether a linked list is circular. Microsoft 11
main() { int x=5; clrscr(); for(;x<= 0;x--) { printf("x=%d ", x--); } } a. 5, 3, 1 b. 5, 2, 1, c. 5, 3, 1, -1, 3 d. 3, -1, 1, 3, 5 HCL 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