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 *ptr=(int*)malloc(sizeof(int)); *ptr=4; printf("%d",(*ptr)+++*ptr++); }
All the combinations of prime numbers whose sum gives 32
how to concatenate the two strings
Write a c program to search an element in an array using recursion
main() { char string[]="Hello World"; display(string); } void display(char *string) { printf("%s",string); }
struct point { int x; int y; }; struct point origin,*pp; main() { pp=&origin; printf("origin is(%d%d)\n",(*pp).x,(*pp).y); printf("origin is (%d%d)\n",pp->x,pp->y); }
find simple interest & compund interest
main() { register int a=2; printf("Address of a = %d",&a); printf("Value of a = %d",a); }
Predict the Output: int main() { int *p=(int *)2000; scanf("%d",2000); printf("%d",*p); return 0; } if input is 20 ,what will be print
Finding a number which was log of base 2
what will be the position of the file marker? a: fseek(ptr,0,SEEK_SET); b: fseek(ptr,0,SEEK_CUR);
main() { int i; printf("%d",scanf("%d",&i)); // value 10 is given as input here }