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() { char *p="GOOD"; char a[ ]="GOOD"; printf("\n sizeof(p) = %d, sizeof(*p) = %d, strlen(p) = %d", sizeof(p), sizeof(*p), strlen(p)); printf("\n sizeof(a) = %d, strlen(a) = %d", sizeof(a), strlen(a)); }
#include"math.h" void main() { printf("Hi everybody"); } if <stdio.h> will be included then this program will must compile, but as we know that when we include a header file in "" then any system defined function find its defination from all the directrives. So is this code of segment will compile? If no then why?
#include<stdio.h> main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s; printf("%d",s->x); printf("%s",s->name); }
main() { char *p = "hello world"; p[0] = 'H'; printf("%s", p); } a. Runtime error. b. “Hello world” c. Compile error d. “hello world”
Finding a number multiplication of 8 with out using arithmetic operator
#include<stdio.h> #include<conio.h> void main() { int a=(1,2,3,(1,2,3,4); switch(a) { printf("ans:"); case 1: printf("1");break; case 2: printf("2");break; case 3: printf("1");break; case 4: printf("4");break; printf("end"); } getch(); }
How will u find whether a linked list has a loop or not?
write a c-program to display the time using FOR loop
Set up procedure for generating a wire frame display of a polyhedron with the hidden edges of the object drawn with dashed lines
main() { char str1[] = {‘s’,’o’,’m’,’e’}; char str2[] = {‘s’,’o’,’m’,’e’,’\0’}; while (strcmp(str1,str2)) printf(“Strings are not equal\n”); }
main() { char *p; p="Hello"; printf("%c\n",*&*p); }
main() { char *p="hai friends",*p1; p1=p; while(*p!='\0') ++*p++; printf("%s %s",p,p1); }