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
Explain how do you determine a file’s attributes?
How to get string length of given string in c?
C program execution always begins with a) #include b) comment (/*-------*/) c) main() d) declaration instructions
Is c dynamically typed?
Is it better to bitshift a value than to multiply by 2?
What is 2 d array in c?
What could possibly be the problem if a valid function name such as tolower() is being reported by the C compiler as undefined?
What is the full form of getch?
What are the types of variables in c?
Why c is a mother language?
How many types of sorting are there in c?
How can I sort a linked list?
How can you return multiple values from a function?
program for reversing a selected line word by word when multiple lines are given without using strrev
Apart from dennis ritchie who the other person who contributed in design of c language.