Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...

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 ?..

Hi this question was asked in my interview.
Ans is :

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

Any other solution?

 Is This Answer Correct ? 1769 Yes 223 No

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

use xor to swap

a = a^b
b= a^b
a= a^b

 Is This Answer Correct ? 547 Yes 165 No

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

Ans is :

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

 Is This Answer Correct ? 536 Yes 185 No

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

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

 Is This Answer Correct ? 302 Yes 64 No

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

x = x + y;

y = x - y;

x = x - y;

 Is This Answer Correct ? 113 Yes 25 No

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

Only the Xor answer (#2) is correct (in cases where the
variables are the same size). With all the other answers,
you could run into over/under flow problems.
A = 01111111
B = 01111101

A = A^B = 00000010
B = A^B = 01111111 (Original A)
A = A^B = 01111101 (Original B)

 Is This Answer Correct ? 94 Yes 21 No

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

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

 Is This Answer Correct ? 119 Yes 77 No

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

2 one will work correct

 Is This Answer Correct ? 83 Yes 45 No

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

void main()
{
int a,b;
printf("Enter two number : ");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("The swap number is : %d %d",a,b);
getch();
}

 Is This Answer Correct ? 45 Yes 11 No

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

Answer no 12 is wrong. the logic fails when a=0;

 Is This Answer Correct ? 44 Yes 17 No

More C Code Interview Questions

main() { void swap(); int x=10,y=8; swap(&x,&y); printf("x=%d y=%d",x,y); } void swap(int *a, int *b) { *a ^= *b, *b ^= *a, *a ^= *b; }

to remove the repeated cahracter from the given caracter array. i.e.., if the input is SSAD output should of SAD

main() { int i, n; char *x = “girl”; n = strlen(x); *x = x[n]; for(i=0; i<n; ++i) { printf(“%s\n”,x); x++; } }

#define int char main() { int i=65; printf("sizeof(i)=%d",sizeof(i)); }

#define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); }

union u { union u { int i; int j; }a; int b; }u; main() { printf("\n%d", sizeof(u)); printf(" %d", sizeof(u.a)); // printf("%d", sizeof(u.a.i)); } a. 4, 4, 4 b. 40, 4, 4 c. 1, 100, 1 d. 40 400 4

print a semicolon using Cprogram without using a semicolon any where in the C code in ur program!!

Write a program to implement the motion of a bouncing ball using a downward gravitational force and a ground-plane friction force. Initially the ball is to be projected in to space with a given velocity vector

Display the time of the system and display the right time of the other country

#include<stdio.h> main() { register i=5; char j[]= "hello"; printf("%s %d",j,i); }

main() { unsigned int i=65000; while(i++!=0); printf("%d",i); }

what is variable length argument list?

Categories
• C Code (423)
• C++ Code (128)
• VC++ Code (17)
• Java Code (84)
• Dot Net Code (114)
• Visual Basic Code (11)
• Programming Code AllOther (62)