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

rahul

Is This Answer Correct ?    0 Yes 1 No

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

Answer / deep

let a=5 , b=10
a=a-b
means a=5-10=-5
b=b+a
b=10+(-5)
b=5
a=b-a
a=5-(-5)
a=10

Is This Answer Correct ?    1 Yes 2 No

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

Answer / beeresh

a=a/b;
b=a*b;
a=b/a;

Is This Answer Correct ?    0 Yes 1 No

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

Answer / d maniteja

let
a=x, b=y our aim is to get output as a=y&b=x;
program:
void main()
{
int a=x,b=y;
a=(a+b)+(a-b);
b=(a+b)-(a-b);
a=a/2;
b=b/2;
printf(("%d %d",a,b);
}

Is This Answer Correct ?    0 Yes 1 No

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

Answer / e.naveenkumar

without using third variable swap two nos
a=(a+b)-(b-a);

Is This Answer Correct ?    0 Yes 1 No

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

Answer / amit kumar

10 answer is perfect

Is This Answer Correct ?    0 Yes 1 No

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

Answer / gobinath

swap(int *a,int *b)
{
*a=*a-*b;
*b=*a+*b;
*a=*b-*a;

}

Is This Answer Correct ?    0 Yes 1 No

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

Answer / sudha

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

Is This Answer Correct ?    0 Yes 1 No

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

Answer / jc10

1. b=b+a
2. a=b-a
3. b=b-a

Is This Answer Correct ?    1 Yes 2 No

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

Answer / suraj

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

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More C Code Interview Questions

What is full form of PEPSI

0 Answers  


plz send me all data structure related programs

2 Answers  


find A^B using Recursive function

2 Answers  


#define FALSE -1 #define TRUE 1 #define NULL 0 main() { if(NULL) puts("NULL"); else if(FALSE) puts("TRUE"); else puts("FALSE"); }

1 Answers  


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

1 Answers  






main() { char str1[] = {‘s’,’o’,’m’,’e’}; char str2[] = {‘s’,’o’,’m’,’e’,’\0’}; while (strcmp(str1,str2)) printf(“Strings are not equal\n”); }

1 Answers  


Hi, i have a project that the teacher want a pyramid of numbers in C# or java...when we click a button...the pyramid should be generated in a listbox/or JtextArea...and the pyramid should have the folowing form: 1 232 34543 4567654 567898765 67890109876 7890123210987 890123454321098 90123456765432109 0123456789876543210 Plz help with codes...didn't find anything on the net.

0 Answers  


void main() { int *mptr, *cptr; mptr = (int*)malloc(sizeof(int)); printf(“%d”,*mptr); int *cptr = (int*)calloc(sizeof(int),1); printf(“%d”,*cptr); }

1 Answers  


#include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); }

1 Answers  


void main() { int *i = 0x400; // i points to the address 400 *i = 0; // set the value of memory location pointed by i; }

2 Answers  


How we print the table of 2 using for loop in c programing?

14 Answers   HCL, Wipro,


what is the output of following program ? void main() { int i=5; printf("%d %d %d %d %d ",i++,i--,++i,--i,i); }

10 Answers  


Categories