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 / poojamnagal
Num1 = 5
Num2 = 7
Num1 = 5+7 = 12
Num2 = 12- 7 = 5
Num1 = 12-5 = 7
So, Now Num1 = 7
Num2 = 5
| Is This Answer Correct ? | 2 Yes | 2 No |
# include <stdio.h> int one_d[]={1,2,3}; main() { int *ptr; ptr=one_d; ptr+=3; printf("%d",*ptr); }
Is the following code legal? struct a { int x; struct a *b; }
int i; main(){ int t; for ( t=4;scanf("%d",&i)-t;printf("%d\n",i)) printf("%d--",t--); } // If the inputs are 0,1,2,3 find the o/p
How do you create a really large matrix (i.e. 3500x3500) in C without having the program crash? I can only reach up to 2500. It must have something to do with lack of memory. Please help!
why the range of an unsigned integer is double almost than the signed integer.
main() { float i=1.5; switch(i) { case 1: printf("1"); case 2: printf("2"); default : printf("0"); } }
Write a program to print a square of size 5 by using the character S.
Write a single line c expression to delete a,b,c from aabbcc
Write a program to receive an integer and find it's octal equivalent. How can i do with using while loop.
main(){ char a[100]; a[0]='a';a[1]]='b';a[2]='c';a[4]='d'; abc(a); } abc(char a[]){ a++; printf("%c",*a); a++; printf("%c",*a); }
main() { int y; scanf("%d",&y); // input given is 2000 if( (y%4==0 && y%100 != 0) || y%100 == 0 ) printf("%d is a leap year"); else printf("%d is not a leap year"); }
x=2 y=3 z=2 x++ + y++; printf("%d%d" x,y);