swap two integer variables without using a third temporary
variable?

Answers were Sorted based on User's Feedback



swap two integer variables without using a third temporary variable?..

Answer / sidhartha

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("\n ENTER 2 VALUES : ");
scanf("%d%d",&a,&b);
printf("\n THE VALUES BEFORE SORTING : %d,%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\n THE VALUES AFTER SORTING : %d,%d",a,b);
getch();
}

Is This Answer Correct ?    40 Yes 1 No

swap two integer variables without using a third temporary variable?..

Answer / jigish

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

simple..try it

Is This Answer Correct ?    15 Yes 5 No

swap two integer variables without using a third temporary variable?..

Answer / vignesh1988i

the best way what i choose is that : if x=89 , y=-88

x^=y^=x^=y;

this line will swap the above numbers......


thank u

Is This Answer Correct ?    10 Yes 5 No

swap two integer variables without using a third temporary variable?..

Answer / sumit salve

a=a+b-(b=a);
or
a=a*b/(b=a);
this will also swap two numbers!!!

Thank You...

Is This Answer Correct ?    0 Yes 0 No

swap two integer variables without using a third temporary variable?..

Answer / onkar koparde

i'll only give u logic #include<stdio.h>
void main() { int a=10,b=20;
printf("b4 swap:a=%d b=%d",a,b);
a=a+b;b=a-b;a=a-b; printf("aftr
swap:a=%d b=%d",a,b); }
from-onkar.koparde@gmail.com

Is This Answer Correct ?    2 Yes 4 No

swap two integer variables without using a third temporary variable?..

Answer / abhinav lohar

t=a;
a=b;
b=t;

Is This Answer Correct ?    0 Yes 8 No

Post New Answer

More C Interview Questions

hi... can anyone help me to make a two-dimensinal arrays in finding the sum of two elements plzzz. thnx a lot...

0 Answers  


Explain the binary height balanced tree?

0 Answers  


Q-1: Create a structure to specify data on students given below: Roll number, Name, Department, Course, Year of joining Assume that there are not more than 450 students in the college.

8 Answers  


can we declare a function in side the structure?

2 Answers   HCL,


Why C language is a procedural language?

0 Answers   Ericsson,






Explain what is a const pointer?

0 Answers  


write a c program to find the roots of a quadratic equation ax2 + bx + c = 0

11 Answers   CSC, St Marys, TATA,


Where are c variables stored in memory?

0 Answers  


What is variable in c with example?

1 Answers  


Define and explain about ! Operator?

0 Answers  


Is there a built-in function in C that can be used for sorting data?

0 Answers  


What is the default value of local and global variables in c?

0 Answers  


Categories