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

b=(a+b)-(a=b);

Is This Answer Correct ?    0 Yes 1 No

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

Answer / shruti dengre

answer no. 6th is is correct

Is This Answer Correct ?    0 Yes 1 No

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

Answer / suseela

for ex x=10,y=20
x=x+y=30(i.e 10+20)
y=x-y=10(i.e 30-20)
x=x-y=20(i.e 30-10)

Is This Answer Correct ?    0 Yes 1 No

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

Answer / shubham

#include<stdio.h>
#imclude<conio.h>
main()
{
int a,b;
printf("enter two number");
scanf("%d%d",&a,&b);
b=a-b;
a=a-b;
b=a+b;
printf("after swaping &d",swap);
getch();
}

Is This Answer Correct ?    0 Yes 1 No

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

Answer / lokesh

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 / praveen kumar telluri

b=a*b/(a=b);

Is This Answer Correct ?    0 Yes 1 No

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

Answer / naveen

Using a single line statement:

a=(a+b)-(b=a)

Is This Answer Correct ?    0 Yes 1 No

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

Answer / hamsa

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

ex a=2, b=4

a=2+4
b=6-4
a=6-2


ANS a=4
b=4

Is This Answer Correct ?    0 Yes 1 No

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

Answer / debjyoti talukder

a=a+b;
if(a>b)//only the smaller can b subtracted from greater...
b=a-b://...reverse is not possible
else
b=b-a;
a=a-b;

Is This Answer Correct ?    0 Yes 1 No

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

Answer / swapnil r

Swapping two variables

1. #define SWAP(x,y) x^=y^=x^=y

2. a=a+b;
b=a-b;
a=a-b;

3. use xor to swap

a = a^b
b= a^b
a= a^b
4. a=a*b;
b=a/b;
a=a/b;

Is This Answer Correct ?    1 Yes 3 No

Post New Answer

More C Code Interview Questions

To reverse an entire text file into another text file.... get d file names in cmd line

0 Answers   Subex,


main() { char *p; int *q; long *r; p=q=r=0; p++; q++; r++; printf("%p...%p...%p",p,q,r); }

1 Answers  


main() { static int var = 5; printf("%d ",var--); if(var) main(); }

1 Answers  


main() { int a=10,*j; void *k; j=k=&a; j++; k++; printf("\n %u %u ",j,k); }

1 Answers  


#define DIM( array, type) sizeof(array)/sizeof(type) main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr, int)); }

1 Answers  


Write a C program to add two numbers before the main function is called.

11 Answers   Infotech, TC,


main() { struct date; struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); }

1 Answers  


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

1 Answers  


main() { int i=0; for(;i++;printf("%d",i)) ; printf("%d",i); }

1 Answers   Zoho,


Write, efficient code for extracting unique elements from a sorted list of array. e.g. (1, 1, 3, 3, 3, 5, 5, 5, 9, 9, 9, 9) -> (1, 3, 5, 9).

13 Answers   Intel, Microsoft, TCS,


# include <stdio.h> int one_d[]={1,2,3}; main() { int *ptr; ptr=one_d; ptr+=3; printf("%d",*ptr); }

1 Answers  


Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it.

2 Answers   Wipro,


Categories