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
# 26
The first two answers are correct. Third will FAIL in the 
case the second num is 0...Please do not  post wrong answer
 
Is This Answer Correct ?    2 Yes 8 No
Ashok
 
  Re: How to swap two variables, without using third variable ?
Answer
# 27
first & third answers are correct.
 
Is This Answer Correct ?    1 Yes 6 No
Sonya
 
  Re: How to swap two variables, without using third variable ?
Answer
# 28
i think first is right.
 
Is This Answer Correct ?    5 Yes 4 No
Sujata
 
  Re: How to swap two variables, without using third variable ?
Answer
# 29
a=a-b
b=b+a
a=b-a


is this wrong?
 
Is This Answer Correct ?    3 Yes 6 No
D
 
  Re: How to swap two variables, without using third variable ?
Answer
# 30
#include<stdio.h>
main()
{
int a,b;
printf("Enter any two numbers\n");
scanf("%d%d",&a,&b);
printf("The values before swapping are\n%d %d\n",a,b);
swap(&a,&b);
printf("The values after swapping are\n%d %d\n",a,b);
getch();
}

swap(*x,*y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
 
Is This Answer Correct ?    0 Yes 10 No
Rehan
 
  Re: How to swap two variables, without using third variable ?
Answer
# 31
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 ?    3 Yes 4 No
Pavan.
 
  Re: How to swap two variables, without using third variable ?
Answer
# 32
Sorry i dont know..??
only 1st ans is write......
so give me positive marking....
   Thank You...
 
Is This Answer Correct ?    4 Yes 2 No
Ankit Goel
 
  Re: How to swap two variables, without using third variable ?
Answer
# 33
if any one of the variables eithet A or B is negative or 
else both r  negatibe then how logic will vary in cobol?
 
Is This Answer Correct ?    1 Yes 0 No
Pallabi
 
  Re: How to swap two variables, without using third variable ?
Answer
# 34
($a,$b)=($b,$a)
....this is how we do in PERL....Ha Ha Ha pretty cool !!!!

And BELIEVE me it works not only for NUMBERS but also for
STRINGS, ARRAYS or any Data Structure or any garbage
value....this is mine Challenge.....

None of the any programming language can come close to PERL
in this much of SIMPLICITY and ROBUSTNESS....just one line
does the Magic....

Again I would say the Question itself is very silly one "How
to swap two variables, without using third variable
?"....and I see alot of stupid Answers posted here....

Everyone posting the answer is assuming that Question is
about swapping INTEGER NUMBERS....I would like to ask what
if I provide FLOATING POINT NUMBERS, NEGATIVE NUMBERS, REAL
NUMBERS, and surely it does not work for STRINGS....

That is why I say "Perl is immensely powerful. If you think
something can't be done, the problem is likely to be it is
beyond your ability, not that of Perl."

Welcome to the world of PERL....its more precious than PEARL....
 
Is This Answer Correct ?    2 Yes 0 No
Perl Guru
 
  Re: How to swap two variables, without using third variable ?
Answer
# 35
#include<stdio.h>
#include<conio.h>
int main()
{
    int a,b;
     clrscr();
    printf("\nEnter two numbers:");
     scanf("%d%d",&a,&b);
     printf("\nThe numbers after swapping are %d  %d",b,a);
      getch();
      return 0;
}
 
Is This Answer Correct ?    0 Yes 1 No
Mangesh
 
  Re: How to swap two variables, without using third variable ?
Answer
# 36
only #2 is d right answer......XOR yields...perfect answers.....
 
Is This Answer Correct ?    0 Yes 1 No
Harish
 
  Re: How to swap two variables, without using third variable ?
Answer
# 37
1st and 2nd methods r right!!
dere iz some problem wid 3rd..if a=0.

18th method...how u guy give dis type of solution?? is it
ryte?? suppose a>b den??
 
Is This Answer Correct ?    0 Yes 0 No
Aditya Raj
 
  Re: How to swap two variables, without using third variable ?
Answer
# 38
void main()
{
  int a,b;
  clrscr();
  printf("\n Enter the values of a and b ");
  scanf(" %d %d ", &a,&b);
  a=a*b;
  b=a/b;
  a=a/b;
  printf("\n \n After swapping ----> a = %d \t b = %d",a,b);
  getch();
}
 
Is This Answer Correct ?    3 Yes 1 No
Bhaskar.mantrala
 
  Re: How to swap two variables, without using third variable ?
Answer
# 39
a=a+b-(a=b);

This is the example by using only one line to swap the two 
variables without using the third variable.
 
Is This Answer Correct ?    2 Yes 0 No
Balasubramanian Ganapthi
 
  Re: How to swap two variables, without using third variable ?
Answer
# 40
a=10;
b=20;
a=a+b;
b=a-b;
a=a-b;
a=20,b=10;
 
Is This Answer Correct ?    0 Yes 0 No
Indu B
 
  Re: How to swap two variables, without using third variable ?
Answer
# 41
declare a fourth variable and use that.
I dont understand why you need to do any of the above ??? if fourth is a problem, declare fifth and so on...
 
Is This Answer Correct ?    0 Yes 1 No
Some Guy
 
  Re: How to swap two variables, without using third variable ?
Answer
# 42
if x=14, y=18
x=x+y;
now x=14+18=32;
y=x-y;
now y=32-18=14;
now again y=14
x=x-y;
now x=32-14=18
final answer is:
x=18, and y=14
u can try this formule by taking any values for x and y.
 
Is This Answer Correct ?    0 Yes 2 No
Saddi Srikanth
 
  Re: How to swap two variables, without using third variable ?
Answer
# 43
a=a-b;
b=a+b;
a=b-a;
 
Is This Answer Correct ?    0 Yes 1 No
Satyaprakash Tripathi
 
  Re: How to swap two variables, without using third variable ?
Answer
# 44
I am new in C/C++.
How to write the program for second answer. Does it
automatically convert the numbers in binary by using ^ sign.

:}
 
Is This Answer Correct ?    0 Yes 0 No
Rahul
 
  Re: How to swap two variables, without using third variable ?
Answer
# 45
a[12]=name
b[20]=name
    Here the size 12 & 25 has no effect hence we can change
the size
a[]=name
b[]=name
 
Is This Answer Correct ?    0 Yes 0 No
Devarajan.k
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
main() { int i =0;j=0; if(i && j++) printf("%d..%d",i++,j); printf("%d..%d,i,j); }  1
void main() { if(~0 == (unsigned int)-1) printf(“You can answer this if you know how values are represented in memory”); }  1
main() { main(); }  1
#include<stdio.h> main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p,*q; p=&a[2][2][2]; *q=***a; printf("%d----%d",*p,*q); }  1
main() { char str1[] = {‘s’,’o’,’m’,’e’}; char str2[] = {‘s’,’o’,’m’,’e’,’\0’}; while (strcmp(str1,str2)) printf(“Strings are not equal\n”); }  1
main() { extern out; printf("%d", out); } int out=100;  1
Give a one-line C expression to test whether a number is a power of 2. Microsoft8
main ( ) { static char *s[ ] = {“black”, “white”, “yellow”, “violet”}; char **ptr[ ] = {s+3, s+2, s+1, s}, ***p; p = ptr; **++p; printf(“%s”,*--*++p + 3); }  1
main(int argc, char *argv[]) { (main && argc) ? main(argc-1, NULL) : return 0; } a. Runtime error. b. Compile error. Illegal syntax c. Gets into Infinite loop d. None of the above HCL1
main() { int i; printf("%d",scanf("%d",&i)); // value 10 is given as input here }  1
main() { char s[ ]="man"; int i; for(i=0;s[ i ];i++) printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]); }  1
Program to Delete an element from a doubly linked list. Infosys4
void pascal f(int i,int j,int k) { printf(“%d %d %d”,i, j, k); } void cdecl f(int i,int j,int k) { printf(“%d %d %d”,i, j, k); } main() { int i=10; f(i++,i++,i++); printf(" %d\n",i); i=10; f(i++,i++,i++); printf(" %d",i); }  1
main() { int a=2,*f1,*f2; f1=f2=&a; *f2+=*f2+=a+=2.5; printf("\n%d %d %d",a,*f1,*f2); }  1
String copy logic in one line. NetApp9
how to delete an element in an array  1
main() { char c; int i = 456; clrscr(); c = i; printf("%d", c); } a. 456 b. -456 c. random number d. none of the above HCL1
Extend the sutherland-hodgman clipping algorithm to clip three-dimensional planes against a regular paralleiepiped IBM1
How to swap two variables, without using third variable ? HCL45
# include<stdio.h> aaa() { printf("hi"); } bbb(){ printf("hello"); } ccc(){ printf("bye"); } main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc; ptr[2](); }  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