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 |
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!
#include <stdio.h> int main(void) { int a=4, b=2; a=b<<a+b>>2 ; printf("%d",a); return 0; }
int a=1; printf("%d %d %d",a++,a++,a); need o/p in 'c' and what explanation too
main() { int a=10,*j; void *k; j=k=&a; j++; k++; printf("\n %u %u ",j,k); }
main() { char *cptr,c; void *vptr,v; c=10; v=0; cptr=&c; vptr=&v; printf("%c%v",c,v); }
Write a Program in 'C' To Insert a Unique Number Only. (Hint: Just Like a Primary Key Numbers In Database.) Please Some One Suggest Me a Better Solution for This question ??
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']
How will u find whether a linked list has a loop or not?
void main() { static int i=i++, j=j++, k=k++; printf(“i = %d j = %d k = %d”, i, j, k); }
how many processes will gate created execution of -------- fork(); fork(); fork(); -------- Please Explain... Thanks in advance..!
main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); }
What are the files which are automatically opened when a C file is executed?