How to swap two variables, without using third variable ?

Answers were Sorted based on User's Feedback



How to swap two variables, without using third variable ?..

Answer / xyz

Congrats to all guys who have tried this.Everything u post
here is correct

Is This Answer Correct ?    0 Yes 0 No

How to swap two variables, without using third variable ?..

Answer / not so good coder

I heard you put them into an excel spreadsheet, and just
move one cell over the other.

Is This Answer Correct ?    0 Yes 0 No

How to swap two variables, without using third variable ?..

Answer / adarsh jain

We can do it using pointers easily...

See the code snippets below..

#include<stdio.h>

void swap(int *a, int *b);
int main()
{
int a = 10;
int b = 20;
swap(&a, &b);
return 0;
}

void swap(int *a, int *b)
{
printf("Before swapping , a = %d, b = %d\n", *a ,
*b);
*(a+1) = *a;
*a = *b;
*b = *(a+1);
printf("After Swapping, a = %d, b = %d\n", *a, *b);
}

Is This Answer Correct ?    0 Yes 0 No

How to swap two variables, without using third variable ?..

Answer / saad bin saulat

Full code to swap variables without using a temporary variable is available at the below mentioned link:
http://bitsbyta.blogspot.com/2011/01/swapping-values-without-third-variable.html

Is This Answer Correct ?    1 Yes 1 No

How to swap two variables, without using third variable ?..

Answer / zee hassan

#include<iostream.h>
#include<conio.h>
main()
{
int a,b;
a=5;
b=10;
a=a+b; /*a=5+10=15*/
b=a-b; /*b=15-10=5*/
a=a-b;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
getch();
}

Is This Answer Correct ?    0 Yes 0 No

How to swap two variables, without using third variable ?..

Answer / swathi

a=5 b=10
b=b-a, a=a+b
b=10-5=5
a=5+5=10
b=5,a=10

Is This Answer Correct ?    1 Yes 1 No

How to swap two variables, without using third variable ?..

Answer / aditi agrawal

a=a-b;
b=a+b;
a=b-a;

Is This Answer Correct ?    1 Yes 1 No

How to swap two variables, without using third variable ?..

Answer / ipsagel

# 3 also

Is This Answer Correct ?    0 Yes 0 No

How to swap two variables, without using third variable ?..

Answer / saurabh singh

a=b^a^(b=a);

Is This Answer Correct ?    0 Yes 0 No

How to swap two variables, without using third variable ?..

Answer / aditya raj

1st and 2nd methods r right!!
dere iz some problem wid 3rd..if a=0.

18th method...how u guy give dis type of solution?? is it
ryte?? suppose a>b den??

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More C Code Interview Questions

main() { char not; not=!2; printf("%d",not); }

1 Answers  


#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(); }

0 Answers  


int i,j; for(i=0;i<=10;i++) { j+=5; assert(i<5); }

3 Answers   Cisco, HCL,


main() { int x=5; for(;x!=0;x--) { printf("x=%d\n", x--); } } a. 5, 4, 3, 2,1 b. 4, 3, 2, 1, 0 c. 5, 3, 1 d. none of the above

2 Answers   HCL,


main() { char *a = "Hello "; char *b = "World"; clrscr(); printf("%s", strcat(a,b)); } a. Hello b. Hello World c. HelloWorld d. None of the above

3 Answers   HCL,






how can i search an element in an array

2 Answers   CTS, Microsoft, ViPrak,


find A^B using Recursive function

2 Answers  


main(int argc, char **argv) { printf("enter the character"); getchar(); sum(argv[1],argv[2]); } sum(num1,num2) int num1,num2; { return num1+num2; }

1 Answers  


Is the following code legal? typedef struct a aType; struct a { int x; aType *b; };

1 Answers  


main() { char *cptr,c; void *vptr,v; c=10; v=0; cptr=&c; vptr=&v; printf("%c%v",c,v); }

1 Answers  


How can I Create a C program in splitting set of characters to specific subsets. Example: INPUT SET OF CHARACTERS: Therefore, my dear brothers and sisters, stand firm. Let nothing move you. Always give yourselves fully to the work of the Lord, because you know that your labor in the Lord is not in vain. SPLIT INTO HOW MANY CHARACTERS PER SUBSETS: 10 OUTPUT: Therefore, my dear b rothers an d sisters, stand fir m. Let not hing move you. Alway s give you rselves fu lly to the work of t he Lord, b ecause you know that your labo r in the L ord is not in vain.

0 Answers  


main() { char c=' ',x,convert(z); getc(c); if((c>='a') && (c<='z')) x=convert(c); printf("%c",x); } convert(z) { return z-32; }

1 Answers  


Categories