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 |
Find the largest number in a binary tree
program to Reverse a linked list
12 Answers Aricent, Microsoft, Ness Technologies,
what is oop?
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
4 Answers Corporate Society, HCL,
String copy logic in one line.
char *someFun() { char *temp = “string constant"; return temp; } int main() { puts(someFun()); }
Give a oneline C expression to test whether a number is a power of 2?
25 Answers EA Electronic Arts, Google, Motorola,
#include<stdio.h> int main() { int a=3,post,pre; post= a++ * a++ * a++; a=3; pre= ++a * ++a * ++a; printf("post=%d pre=%d",post,pre); return 0; }
What is the main difference between STRUCTURE and UNION?
How we print the table of 3 using for loop in c programing?
How do you verify if the two sentences/phrases input is an anagram using predefined functions in string.h and by using arrays?
main( ) { int a[ ] = {10,20,30,40,50},j,*p; for(j=0; j<5; j++) { printf(“%d” ,*a); a++; } p = a; for(j=0; j<5; j++) { printf(“%d ” ,*p); p++; } }