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() { int i; clrscr(); for(i=0;i<5;i++) { printf("%d\n", 1L << i); } } a. 5, 4, 3, 2, 1 b. 0, 1, 2, 3, 4 c. 0, 1, 2, 4, 8 d. 1, 2, 4, 8, 16
Finding a number which was log of base 2
Write a C program to print ‘Campus Force training’ without using even a single semicolon in the program.
main() { if ((1||0) && (0||1)) { printf("OK I am done."); } else { printf("OK I am gone."); } } a. OK I am done b. OK I am gone c. compile error d. none of the above
Write, efficient code for extracting unique elements from a sorted list of array. e.g. (1, 1, 3, 3, 3, 5, 5, 5, 9, 9, 9, 9) -> (1, 3, 5, 9).
13 Answers Intel, Microsoft, TCS,
Is the following code legal? struct a { int x; struct a b; }
Given a spherical surface, write bump-mapping procedure to generate the bumpy surface of an orange
To Write a C program to remove the repeated characters in the entered expression or in entered characters(i.e) removing duplicates. String contains only lowercase characters ['a'-'z']
#include<stdio.h> main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; }
void main() { char far *farther,*farthest; printf("%d..%d",sizeof(farther),sizeof(farthest)); }
What are the following notations of defining functions known as? i. int abc(int a,float b) { /* some code */ } ii. int abc(a,b) int a; float b; { /* some code*/ }
String reverse with time complexity of n/2 with out using temporary variable.