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 ?    133 Yes 16 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 ?    53 Yes 19 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 ?    34 Yes 26 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 ?    18 Yes 35 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 ?    8 Yes 16 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 ?    36 Yes 6 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 ?    9 Yes 10 No
P.muthukumar
 
  Re: How to swap two variables, without using third variable ?
Answer
# 8
2 one will work correct
 
Is This Answer Correct ?    14 Yes 3 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 ?    6 Yes 10 No
Koneru Gowtham
 
  Re: How to swap two variables, without using third variable ?
Answer
# 10
a=b-a+(b=a);
 
Is This Answer Correct ?    7 Yes 14 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 ?    19 Yes 5 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 16 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 ?    9 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 ?    18 Yes 4 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 ?    10 Yes 4 No
Shubham
 
  Re: How to swap two variables, without using third variable ?
Answer
# 16
x = y | (y=x);
 
Is This Answer Correct ?    3 Yes 14 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 13 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 10 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 ?    3 Yes 8 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 7 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 ?    6 Yes 4 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 2 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 ?    0 Yes 6 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 ?    5 Yes 6 No
Hanmanth Reddy
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
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... Oracle3
program to Reverse a linked list Ness-Technologies3
How can u say that a given point is in a triangle? 1. with the co-ordinates of the 3 vertices specified. 2. with only the co-ordinates of the top vertex given.  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. Microsoft4
Write a routine that prints out a 2-D array in spiral order Microsoft2
#include<stdio.h> void fun(int); int main() { int a; a=3; fun(a); printf("\n"); return 0; } void fun(int i) { if(n>0) { fun(--n); printf("%d",n); fun(--n); } } the answer is 0 1 2 0..someone explain how the code is executed..?  1
Write a program that find and print how many odd numbers in a binary tree  1
Give a oneline C expression to test whether a number is a power of 2? Motorola13
What is the main difference between STRUCTURE and UNION?  4
program to find magic aquare using array HCL3
Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba. You can assume that all the characters will be unique. Microsoft3
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
How to return multiple values from a function?  4
Write a function to find the depth of a binary tree. Adobe8
Write a procedure to implement highlight as a blinking operation  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) Microsoft4
why java is platform independent? Wipro9
Program to Delete an element from a doubly linked list. Infosys4
Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it. Microsoft4
Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it. Wipro2
 
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