How to swap two variables, without using third variable ?
Answers were Sorted based on User's Feedback
Answer / swapna
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 ? | 1770 Yes | 223 No |
Answer / bruce tuskey
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 ? | 95 Yes | 21 No |
Answer / pavan.
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 ? | 46 Yes | 12 No |
Answer / yash
Answer no 12 is wrong. the logic fails when a=0;
| Is This Answer Correct ? | 44 Yes | 17 No |
main() { int i; clrscr(); printf("%d", &i)+1; scanf("%d", i)-1; } a. Runtime error. b. Runtime error. Access violation. c. Compile error. Illegal syntax d. None of the above
void main() { int i=i++,j=j++,k=k++; printf(ā%d%d%dā,i,j,k); }
write a c-program to display the time using FOR loop
void main() { static int i; while(i<=10) (i>2)?i++:i--; printf(ā%dā, i); }
main() { int c=- -2; printf("c=%d",c); }
write a c program to Reverse a given string using string function and also without string function
int a=1; printf("%d %d %d",a++,a++,a); need o/p in 'c' and what explanation too
what is oop?
How can i find first 5 natural Numbers without using any loop in c language????????
Write a routine that prints out a 2-D array in spiral order
how to delete an element in an array
void main() { int i; char a[]="\0"; if(printf("%s\n",a)) printf("Ok here \n"); else printf("Forget it\n"); }