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
Write a program in c to replace any vowel in a string with z?
Why is python slower than c?
Why is #define used?
write a program to concatenation the string using switch case?
Write a C Program That Will Count The Number Of Even And Odd Integers In A Set using while loop
What is the difference between far and near in c?
number of times a digit is present in a number
What is c preprocessor mean?
any limit on the number of functions that might be present in a C program a) max 35 functions b) max 50 functions c) no limit d) none of the above
How can you check to see whether a symbol is defined?
What is variable and explain rules to declare variable in c?
How would you rename a function in C?
In C language, the variables NAME, name, and Name are all the same. TRUE or FALSE?
explain what is a newline escape sequence?
Hi how many types of software editions are there and their difference (like home editions, enterprise, standard etc) can u please help me