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 / 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

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

Answer / saddi srikanth

if x=14, y=18
x=x+y;
now x=14+18=32;
y=x-y;
now y=32-18=14;
now again y=14
x=x-y;
now x=32-14=18
final answer is:
x=18, and y=14
u can try this formule by taking any values for x and y.

Is This Answer Correct ?    4 Yes 5 No

Post New Answer

More C Code Interview Questions

how to return a multiple value from a function?

2 Answers   Wipro,


Write a single line c expression to delete a,b,c from aabbcc

2 Answers   Microsoft,


How to access command-line arguments?

4 Answers  


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().

2 Answers  


C statement to copy a string without using loop and library function..

2 Answers   Persistent, TCS,






#if something == 0 int some=0; #endif main() { int thing = 0; printf("%d %d\n", some ,thing); }

1 Answers  


#include<stdio.h> int main() { int x=2,y; y=++x*x++*++x; printf("%d",y); } Output for this program is 64. can you explain how this output is come??

1 Answers  


main() { char *p; p="Hello"; printf("%c\n",*&*p); }

1 Answers  


1 o 1 1 0 1 0 1 0 1 1 0 1 0 1 how to design this function format in c-language ?

2 Answers  


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

1 Answers  


main() { int i=10,j=20; j = i, j?(i,j)?i:j:j; printf("%d %d",i,j); }

2 Answers   Adobe, CSC,


Write a Program that Inputs 10 Numbers in an Array and Show the Maximum Number

2 Answers   Ace Info,


Categories