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

.find the output of the following program? char*myfunc(char*ptr) { ptr +=3; return (ptr); } int main() { char*x,*y; x="HELLO"; y=myfunc(x); printf("y = %s ",y); return 0; }

0 Answers  


progrem to generate the following series 1 12 123 1234 12345

6 Answers   HCL, Wipro,


write a program that finds the factorial of a number using recursion?

13 Answers   Infosys, TATA,


Why do we use stdio h and conio h?

0 Answers  


what is difference between array and structure?

44 Answers   College School Exams Tests, CTS, Google, HCL, IBM, Motorola, TCS,






yogesh patil in dell

3 Answers   DELL,


What is period operator in c?

3 Answers   Wipro,


Is that possible to store 32768 in an int data type variable?

0 Answers  


Given a piece of code int x[10]; int *ab; ab=x; To access the 6th element of the array which of the following is incorrect? (A) *(x+5) (B) x[5] (C) ab[5] (D) *(*ab+5} .

2 Answers   Oracle,


design and implement a program that reads floating-points numbers in a sentinel-controlled loop until the user terminates the program by entering zero.your program should determinate and print the smallest,largest and average of the supplied numbers.

2 Answers  


which one low Priority in c? a)=,b)++,c)==,d)+

10 Answers  


Is this program statement valid? INT = 10.50;

0 Answers  


Categories