How many ways are there to swap two numbers without using
temporary variable? Give the each logic.
Answer Posted / tumatij
/*5. Write a C program to swap two integer numbers in various different techniques.
Answer:
*/
#include<stdio.h>
int main()
{
int a,b,temp;
printf("Enter the two numbers:");
scanf("%d%d",&a,&b);
printf("
Values before swapping a=%d b=%d",a,b);
//First logic
temp=a;
a=b;
b=temp;
printf("
Values after swapping a=%d b=%d",a,b);
//2nd logic
a=a+b;
b=a-b;
a=a-b;
printf("
Values after swapping a=%d b=%d",a,b);
//3rd logic
a=a*b;
b=a/b;
a=a/b;
printf("
Values after swapping a=%d b=%d",a,b);
//4th logic
a=(a+b)-(b=a);
printf("
Values after swapping a=%d b=%d",a,b);
//5th logic
a=a^b;
b=a^b;
a=a^b;
printf("
Values after swapping a=%d b=%d",a,b);
//6th logic
a^=b^=a^=b;
printf("
Values after swapping a=%d b=%d",a,b);
//7th logic
a=a-b;
b=a+b;
a=b-a;
printf("
Values after swapping a=%d b=%d",a,b);
}
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
What are header files in c programming?
What are the rules for the identifier?
What are the types of pointers?
Tell us the use of fflush() function in c language?
what are the advanced features of functions a) function declaration and prototypes b) calling functions by value or by reference c) recursion d) all the above
hi any body pls give me company name interview conduct "c" language only
What is the use of bit field?
What is oops c?
Explain what is the purpose of "extern" keyword in a function declaration?
Why c is procedure oriented?
What does 3 mean in texting?
What is declaration and definition in c?
What is exit() function?
Explain how do you determine a file’s attributes?
application areas a 'c' a) operating system b) graphics, interpreter, assembler c) program evalution, communication softwares d) all the above