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 |
int swap(int *a,int *b) { *a=*a+*b;*b=*a-*b;*a=*a-*b; } main() { int x=10,y=20; swap(&x,&y); printf("x= %d y = %d\n",x,y); }
write a program in c to merge two array
main() { char a[4]="HELLO"; printf("%s",a); }
Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it.
main() { int c = 5; printf("%d", main||c); } a. 1 b. 5 c. 0 d. none of the above
#include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); }
main() { int i=10; void pascal f(int,int,int); f(i++,i++,i++); printf(" %d",i); } void pascal f(integer :i,integer:j,integer :k) { write(i,j,k); }
void main() { int i=i++,j=j++,k=k++; printf(ā%d%d%dā,i,j,k); }
main() { int i = 100; clrscr(); printf("%d", sizeof(sizeof(i))); } a. 2 b. 100 c. 4 d. none of the above
main() { char c; int i = 456; clrscr(); c = i; printf("%d", c); } a. 456 b. -456 c. random number d. none of the above
You are given any character string. Find the number of sets of vowels that come in the order of aeiou in the given string. For eg., let the given string be DIPLOMATIC. The answer returned must be "The number of sets is 2" and "The sets are "IO and AI". Vowels that form a singleton set must be neglected. Try to post the program executable in gcc or g++ or in java.
Code for 1>"ascii to string" 2>"string to ascii"
1 Answers Aricent, Global Logic,