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( ) { int a[ ] = {10,20,30,40,50},j,*p; for(j=0; j<5; j++) { printf(“%d” ,*a); a++; } p = a; for(j=0; j<5; j++) { printf(“%d ” ,*p); p++; } }

1 Answers  


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

1 Answers  


#include<stdio.h> main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; }

1 Answers  


main() { int i=5,j=10; i=i&=j&&10; printf("%d %d",i,j); }

1 Answers  


main() { 41printf("%p",main); }8

1 Answers  






write a c program to input initial & final time in the format hh:mm and find the time intervel between them? Ex inputs are initial 06:30 final 00:05 and 23:22 final 22.30

0 Answers  


void pascal f(int i,int j,int k) { printf(“%d %d %d”,i, j, k); } void cdecl f(int i,int j,int k) { printf(“%d %d %d”,i, j, k); } main() { int i=10; f(i++,i++,i++); printf(" %d\n",i); i=10; f(i++,i++,i++); printf(" %d",i); }

1 Answers  


void main() { char a[]="12345\0"; int i=strlen(a); printf("here in 3 %d\n",++i); }

3 Answers  


Is it possible to print a name without using commas, double quotes,semi-colons?

7 Answers  


main() { int i=400,j=300; printf("%d..%d"); }

3 Answers  


main() { char a[4]="HELLO"; printf("%s",a); }

3 Answers   CSC,


/*what is the output for*/ void main() { int r; printf("Naveen"); r=printf(); getch(); }

4 Answers  


Categories