How to swap two variables, without using third variable ?
Answers were Sorted based on User's Feedback
Answer / marutikutre
x = y | (y=x)-x;
&
a=a*b/(b=a); provided either of a or b is not 0
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / mrx
In assembly language you could use "xchg" instruction witch
swaps the two variables in one instruction.
/* swaps %dest <-> %source */
xchg %dest, %source
/* inline assambly example for C language */
void swap(int register *x, register int *y)
{
asm("xchg %0, %1;" \
: "=r"(*x), "=r"(*y)
: "0"(*x), "1"(*y)
);
}
This is the fastest method you can achieve in hand
optimization. I think C compiler will do the same trick if
possible.
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / simhachalam lakkakula
Answer 4 get fail for the following values
Before: a= 219455078, b= -747788332
After: a= -875186568, b= 1
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / simhachalam lakkakula
Answer 4
Logic:
a=a*b;
b=a/b;
a=a/b;
get fail for the following values
Before: a= 219455078, b= -747788332
After: a= -875186568, b= 1
Is This Answer Correct ? | 1 Yes | 0 No |
---------------------
a=a*b
b=a/b
a=a/b
---------------------
a=a+b
b=a-b
a=a-b
----------------------
a = a Xor b
b= a Xor b
a= a Xor b
----------------------
all above three methods are correct, i hv checked...
Is This Answer Correct ? | 2 Yes | 1 No |
Answer / rahul
I am new in C/C++.
How to write the program for second answer. Does it
automatically convert the numbers in binary by using ^ sign.
:}
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / devarajan.k
a[12]=name
b[20]=name
Here the size 12 & 25 has no effect hence we can change
the size
a[]=name
b[]=name
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / xyz
Congrats to all guys who have tried this.Everything u post
here is correct
Is This Answer Correct ? | 0 Yes | 0 No |
Code for 1>"ascii to string" 2>"string to ascii"
1 Answers Aricent, Global Logic,
#define clrscr() 100 main() { clrscr(); printf("%d\n",clrscr()); }
given integer number,write a program that displays the number as follows: First line :all digits second line : all except the first digit . . . . Last line : the last digit
main() { static int a[3][3]={1,2,3,4,5,6,7,8,9}; int i,j; static *p[]={a,a+1,a+2}; for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%d\t%d\t%d\t%d\n",*(*(p+i)+j), *(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i)); } }
Write a routine that prints out a 2-D array in spiral order
Write a prog to accept a given string in any order and flash error if any of the character is different. For example : If abc is the input then abc, bca, cba, cab bac are acceptable, but aac or bcd are unacceptable.
how many processes will gate created execution of -------- fork(); fork(); fork(); -------- Please Explain... Thanks in advance..!
write a c program to print magic square of order n when n>3 and n is odd?
main() { char *str1="abcd"; char str2[]="abcd"; printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd")); }
main() { printf("\nab"); printf("\bsi"); printf("\rha"); }
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++; } }
void main() { char ch; for(ch=0;ch<=127;ch++) printf(“%c %d \n“, ch, ch); }