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       Ask Questions on ANYTHING, that arise in your Daily Life at     FORUM9.COM
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 ?    230 Yes 25 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 ?    87 Yes 29 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 ?    62 Yes 39 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 54 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 ?    46 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 ?    11 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 ?    13 Yes 19 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 ?    29 Yes 6 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 20 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 ?    13 Yes 5 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 17 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 ?    2 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 22 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 12 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
 
In the following pgm add a stmt in the function fun such that the address of 'a' gets stored in 'j'. main(){ int * j; void fun(int **); fun(&j); } void fun(int **k) { int a =0; /* add a stmt here*/ }  1
write the function. if all the character in string B appear in string A, return true, otherwise return false. Google10
typedef struct error{int warning, error, exception;}error; main() { error g1; g1.error =1; printf("%d",g1.error); }  1
main() { register int a=2; printf("Address of a = %d",&a); printf("Value of a = %d",a); }  1
void main() { int i=5; printf("%d",i++ + ++i); }  1
# include <stdio.h> int one_d[]={1,2,3}; main() { int *ptr; ptr=one_d; ptr+=3; printf("%d",*ptr); }  1
main() { char i=0; for(;i>=0;i++) ; printf("%d\n",i); }  1
program to find the roots of a quadratic equation HP3
You are given any character string. Find the number of sets of vowels that come in the order of aeiou in the given string. For eg., let the given string be DIPLOMATIC. The answer returned must be "The number of sets is 2" and "The sets are "IO and AI". Vowels that form a singleton set must be neglected. Try to post the program executable in gcc or g++ or in java.  3
void main () { int x = 10; printf ("x = %d, y = %d", x,--x++); } a. 10, 10 b. 10, 9 c. 10, 11 d. none of the above HCL1
main() { char *a = "Hello "; char *b = "World"; clrscr(); printf("%s", strcpy(a,b)); } a. “Hello” b. “Hello World” c. “HelloWorld” d. None of the above HCL1
#include<stdio.h> main() { const int i=4; float j; j = ++i; printf("%d %f", i,++j); }  1
void main() { int i=i++,j=j++,k=k++; printf(“%d%d%d”,i,j,k); }  1
main() { int i=-1,j=-1,k=0,l=2,m; m=i++&&j++&&k++||l++; printf("%d %d %d %d %d",i,j,k,l,m); }  1
programming in c lanugaue programm will errror error with two header file one as stdio.h and other one is conio.h  1
main() { unsigned int i=65000; while(i++!=0); printf("%d",i); }  1
main() { int i = 257; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); }  1
main() { int x=5; for(;x!=0;x--) { printf("x=%d\n", x--); } } a. 5, 4, 3, 2,1 b. 4, 3, 2, 1, 0 c. 5, 3, 1 d. none of the above HCL1
Program to find the largest sum of contiguous integers in the array. O(n)  7
char *someFun1() { char temp[ ] = “string"; return temp; } char *someFun2() { char temp[ ] = {‘s’, ‘t’,’r’,’i’,’n’,’g’}; return temp; } int main() { puts(someFun1()); puts(someFun2()); }  1
 
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