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 |
How to use power function under linux environment.eg : for(i=1;i<=n;i++){ pow(-1,i-1)} since it alerts undefined reference to 'pow'.
void main() { int const * p=5; printf("%d",++(*p)); }
3 Answers Infosys, Made Easy, State Bank Of India SBI,
Write a program to receive an integer and find its octal equivalent?
Declare an array of N pointers to functions returning pointers to functions returning pointers to characters?
write a c program to Create a mail account by taking the username, password, confirm password, secret_question, secret_answer and phone number. Allow users to register, login and reset password(based on secret question). Display the user accounts and their details .
#define assert(cond) if(!(cond)) \ (fprintf(stderr, "assertion failed: %s, file %s, line %d \n",#cond,\ __FILE__,__LINE__), abort()) void main() { int i = 10; if(i==0) assert(i < 100); else printf("This statement becomes else for if in assert macro"); }
1. const char *a; 2. char* const a; 3. char const *a; -Differentiate the above declarations.
main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); }
What is the output of the program given below main() { signed char i=0; for(;i>=0;i++) ; printf("%d\n",i); }
void main() { void *v; int integer=2; int *i=&integer; v=i; printf("%d",(int*)*v); }
Write a C program that defines a 2-dimentional integer array called A [50][50]. Then the elements of this array should randomly be initialized either to 1 or 0. The program should then print out all the elements in the diagonal (i.e. a[0][0], a[1][1],a[2][2], a[3][3], ……..a[49][49]). Finally, print out how many zeros and ones in the diagonal.
main() { char p[ ]="%d\n"; p[1] = 'c'; printf(p,65); }