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 does *p++ do? What does it point to?
How can you invoke another program from within a C program?
Can you write the algorithm for Queue?
why programs in c are running with out #include
What is volatile keyword in c?
Can we change the value of constant variable in c?
WHAT IS THE DEFINATION OF IN TECHNOLOGY AND OFF TECHNOLOGY ?
Difference between MAC vs. IP Addressing
What is a MAC Address?
What is time null in c?
What are the uses of null pointers?
I need a sort of an approximate strcmp routine?
What are loops c?
What is the use of getchar() function?
The number of measuring units from an arbitarary starting point in a record,area,or control block to some other point a) recording pointer b) offset c) branching d) none