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                      
info       Did you received any Funny E-Mails from your Friends and like to share with rest of our friends? Yeah!! you can post that stuff   HERE
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C
 
 


 

 
 C interview questions  C Interview Questions
 C++ interview questions  C++ Interview Questions
 VC++ interview questions  VC++ Interview Questions
 Delphi interview questions  Delphi Interview Questions
 Programming Languages AllOther interview questions  Programming Languages AllOther Interview Questions
Question
How do we swap or interchange any 2 numbers without using 
Temporary variable...Anybody can pls answer it.. Thanks in 
Advance
 Question Submitted By :: Venkat
I also faced this Question!!     Rank Answer Posted By  
 
  Re: How do we swap or interchange any 2 numbers without using Temporary variable...Anybody can pls answer it.. Thanks in Advance
Answer
# 1
int x,y;

x =x-y;
y =y+x;           
x =y-x;

or

int x,y;

x=x^y;
y=x^y;
x=x^y;
 
Is This Answer Correct ?    8 Yes 2 No
Raghuram.A
 
  Re: How do we swap or interchange any 2 numbers without using Temporary variable...Anybody can pls answer it.. Thanks in Advance
Answer
# 2
main()
{
int a=16,b=19;
a=a+b;
b=a-b;
a=a-b;
printf("a and b are:%d\t%d",a,b);
}

Output:
a and b are:19  16
 
Is This Answer Correct ?    7 Yes 2 No
Manju
 
 
 
  Re: How do we swap or interchange any 2 numbers without using Temporary variable...Anybody can pls answer it.. Thanks in Advance
Answer
# 3
Logically it is not possible. so your question is worng.
 
Is This Answer Correct ?    1 Yes 6 No
Mangala Pandi
 
  Re: How do we swap or interchange any 2 numbers without using Temporary variable...Anybody can pls answer it.. Thanks in Advance
Answer
# 4
using bitwise operator works 100%:
x^=y;
y^=x;
x^=y;

using +/- works sometime, need to consider overflow.
 
Is This Answer Correct ?    3 Yes 1 No
Emperor Of America
 
  Re: How do we swap or interchange any 2 numbers without using Temporary variable...Anybody can pls answer it.. Thanks in Advance
Answer
# 5
int x,y;

x =x+y;
y =x-y;           
x =x-y;
 
Is This Answer Correct ?    7 Yes 1 No
Surendra Jhajhra
 
  Re: How do we swap or interchange any 2 numbers without using Temporary variable...Anybody can pls answer it.. Thanks in Advance
Answer
# 6
int x,y;

x =x-y;
y =y+x;           
x =y-x;

or

int x,y;

x=x^y;
y=x^y;
x=x^y;
  
or

int x,y;
x*=xy;
y=x/y;
x=x/y;
 
Is This Answer Correct ?    4 Yes 0 No
Srikanth
 
  Re: How do we swap or interchange any 2 numbers without using Temporary variable...Anybody can pls answer it.. Thanks in Advance
Answer
# 7
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a,b;
clrscr();
printf("Enter the two values:");
scanf("%d%d",&a,&b);
swap(a,b);
pritnf("the swaping of given two values are:\na=%d,b=%
d",a,b);
getch();
}
 
Is This Answer Correct ?    0 Yes 0 No
Girish
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
could u able to tell about suresoft technical session  1
#define MAX(x,y) (x) > (y) ? (x) : (y) main() { int i = 10, j = 5, k = 0; k = MAX(i++, ++j); printf("%d %d %d", i,j,k); } what will the values of i , j and k? } NDS9
void main(int argc,char *argv[],char *env[]) { int i; for(i=1;i<argc;i++) printf("%s",env[i]); }  2
which one is highest Priority in c? a)=,b)+,c)++,d)==  3
without using arithmatic operator convert an intger variable x into x+1  1
how we can make 3d venturing graphics on outer interface Microsoft1
Write a C program to print 1 2 3 ... 100 without using loops?  5
Why the use of alloca() is discouraged? Oracle2
When you call malloc() to allocate memory for a local pointer, do you have to explicitly free() it?  2
the format specified for hexa decimal is a.%d b.%o c.%x d.%u TCS4
wats the diference btwen constant pointer and pointer to a constant.pls give examples.  6
what is the function of void main()?  6
What will be printed as the result of the operation below: #include<..> int x; int modifyvalue() { return(x+=10); } int changevalue(int x) { return(x+=1); } void main() { int x=10; x++; changevalue(x); x++; modifyvalue(); printf("First output:%d\n",x); x++; changevalue(x); printf("Second output:%d\n",x); modifyvalue(); printf("Third output:%d\n",x); }  2
which of the following go out of the loopo if expn 2 becoming false a.while(expn 1){...if(expn 2)continue;} b.while(!expn 1){if(expn 2)continue;...} c.do{..if(expn 1)continue;..}while(expn 2); d.while(!expn 2){if(expn 1)continue;..} TCS2
Determine if a number is a power of 2 at O(1).  1
a memory of 20 bytes is allocated to a string declared as char *s then the following two statements are executed: s="Etrance" l=strlen(s); what is the value of l ? a.20 b.8 c.9 d.21 TCS4
what is self refrential structure HCL2
What's the best way to declare and define global variables?  5
why should i select you? Wipro18
int *p=20; if u print like dis printf("%d",p); o\p:- 20; how is it possible? plz give me the explanation. Global-Edge11
 
For more C 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