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

what is the output for this question: main() { int i=1; printf("%d%d%d",i,i++,++i); }

9 Answers  


What is the exact difference between '\0' and ""

3 Answers  


What is the main difference between calloc () and malloc ()?

0 Answers  


Write a C Program to display the following menu: Menu 1. Display 2. Copy 3. Append 4. Exit Accept the choice (1-4) from the user, and perform the following tasks: Choice 1: Accept a file name from the user and display the file on screen Choice 2: Accept two file names, and copy first file to the second Choice 3: Accept two file names, and append second file to the first file Choice 4: Terminate the program

1 Answers   Accenture, Concor, DMU, Satyam, Syntel, Tora,


What does the c preprocessor do?

0 Answers  






Write an implementation of “float stringToFloat(char *str).” The code should be simple, and not require more than the basic operators (if, for, math operators, etc.). • Assumptions • Don’t worry about overflow or underflow • Stop at the 1st invalid character and return the number you have converted till then, if the 1st character is invalid return 0 • Don’t worry about exponential (e.g. 1e10), instead you should treat ‘e’ as an invalid character • Write it like real code, e.g. do error checking • Go though the string only once • Examples • “1.23” should return 1.23 • “1a” should return 1 • “a”should return 0

6 Answers   Qualcomm,


What are the different types of endless loops?

0 Answers  


What is call by reference in functions?

1 Answers  


Explain what is the heap?

0 Answers  


define string ?

0 Answers  


What are the storage classes in C?

0 Answers  


what is the output of the following program? main() { int i=-1,j=-1,k=0,l=2,m; m=i++&&j++&&k++||l++; printf("%d %d %d %d %d",i,j,k,l,m); }

7 Answers  


Categories