How to swap two variables, without using third variable ?
Answers were Sorted based on User's Feedback
Answer / harish
only #2 is d right answer......XOR yields...perfect answers.....
Is This Answer Correct ? | 4 Yes | 1 No |
Answer / balasubramanian ganapthi
a=a+b-(a=b);
This is the example by using only one line to swap the two
variables without using the third variable.
Is This Answer Correct ? | 4 Yes | 2 No |
Answer / xeon
To note on the answer about Perl, it uses a third variable
and just doesn't tell you. The C++ equivalent is:
//Our data
class exampleclass {};
exampleclass classa, classb;
int inta, intb;
float floata, floatb;
//Our swap function
template<class T>
void swap(T *a, T *b) {
T temp = *a;
a = b;
*b = temp;
}
//Our use of the swap function
swap<classexample>(classa, classb);
swap<int>(inta, intb);
swap<float>(floata, floatb);
And also note, Perl is an interpreted (Script) language, so
though it is simple and robust, it is less efficient than a
language that is compiled.
Is This Answer Correct ? | 2 Yes | 0 No |
Answer / ankit goel
Sorry i dont know..??
only 1st ans is write......
so give me positive marking....
Thank You...
Is This Answer Correct ? | 8 Yes | 7 No |
Answer / pallabi
if any one of the variables eithet A or B is negative or
else both r negatibe then how logic will vary in cobol?
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / sriram
Hey guys, what about swaping variables contains string
values? All of your solutions will suck... Try this and swap
any datatype without third variable... Happy Sensible Coding..
$v = 'sriram';
$u = 'lakshmi';
$v .= $u;
$u = substr($v,0,(strlen($v) - strlen($u)));
$v = substr($v,(strlen($v) - strlen($u)-1), strlen($v));
echo 'u = ' . $u .'<br>';
echo 'v = ' . $v;
Is This Answer Correct ? | 1 Yes | 0 No |
main() { extern out; printf("%d", out); } int out=100;
main() { int i = 0xff ; printf("\n%d", i<<2); } a. 4 b. 512 c. 1020 d. 1024
How do I write a program to print proper subset of given string . Eg :input: abc output:{},{a},{b},{c},{a,b},{a,c},{b,c}, {a,b,c}.I desperately need this program please mail me to saravana6m@gmail.com
int a=1; printf("%d %d %d",a++,a++,a); need o/p in 'c' and what explanation too
main() { int i; float *pf; pf = (float *)&i; *pf = 100.00; printf("\n %d", i); } a. Runtime error. b. 100 c. Some Integer not 100 d. None of the above
why the range of an unsigned integer is double almost than the signed integer.
char *someFun() { char *temp = “string constant"; return temp; } int main() { puts(someFun()); }
main() { int c=- -2; printf("c=%d",c); }
main(int argc, char *argv[]) { (main && argc) ? main(argc-1, NULL) : return 0; } a. Runtime error. b. Compile error. Illegal syntax c. Gets into Infinite loop d. None of the above
How to return multiple values from a function?
int main() { int x=10; printf("x=%d, count of earlier print=%d", x,printf("x=%d, y=%d",x,--x)); getch(); } ================================================== returns error>> ld returned 1 exit status =================================================== Does it have something to do with printf() inside another printf().
main() { int i; clrscr(); printf("%d", &i)+1; scanf("%d", i)-1; } a. Runtime error. b. Runtime error. Access violation. c. Compile error. Illegal syntax d. None of the above